当前位置:网站首页>MySQL compound query (important)
MySQL compound query (important)
2022-07-29 02:54:00 【GSX_ MI】
Catalog
1. Basic query review
(1) Check that the salary is higher than 500 Or position is MANAGER Of employees , At the same time, their initials should be capitalized J

(2) Sort by department number in ascending order and employee's salary in descending order

(3) Use annual salary to sort in descending order

(4) Displays the name and position of the highest paid employee

(5) Displays information about employees whose wages are higher than the average

(6) Displays the average wage and maximum wage for each department

(7) It shows that the average wage is lower than 2000 Your department number and its average salary

(8) Displays the total number of employees for each position , Average wage

2. Multi-table query
- In actual development, data often comes from different tables , Therefore, you need to query multiple tables . In this section, we use a simple company management system , There are three tables EMP,DEPT,SALGRADE To demonstrate how to query multiple tables .
(1) Show employee name 、 The employee's salary and the name of the Department, because the above data comes from EMP and DEPT surface , Therefore, joint query


① In fact, we only need emp In the table deptno = dept In the table deptno Field records
② The cartesian product
- generally speaking , Our subsequent multi table queries , Should be a subset of the new table formed by Cartesian product !

(2) The display department number is 10 The name of your department , Employee name and salary

(3) Show the name of each employee , Wages , And salary level

3. Self join
(1) Self join refers to joining queries in the same table , A table can be Cartesian with others , Of course, you can also product with your own Cartesian

(2) Show employees FORD The number and name of the superior (mgr Is the employee leader's number --empno)
① Use subquery

② Use multi table query ( Self query )
- Use the alias to the table
- from emp leader, emp worker, Alias your watch , Because we have to do Cartesian product first , So aliases can be identified first

4. Subquery
- Subqueries are embedded in other sql Statement select sentence , Also called nested query
(1) Single line sub query ( The result of the subquery is a single line )
- Show SMITH Employees in the same department

(2) Multi line sub query
①in keyword ; Query and 10 The name of the employee with the same position in department No , Position , Wages , Department number , But it doesn't include 10 Their own
- First find 10 Position in department No

- Find again and 10 The name of the employee with the same position in department No , Position , Wages , Department number

- Finally, the department number is deleted as 10 The data of

②all keyword ; Show salary ratio Department 30 The names of all the employees with high salaries 、 Salary and department number

③any keyword ; Show salary ratio Department 30 Name of any employee with a higher salary 、 Salary and department number ( Including employees in their own department )


(3) Multi column subquery
- A single row subquery means that a subquery returns only a single column , Single line data ; Multi row sub query is to return single column and multi row data , It's all about single columns , Multi column subquery refers to the subquery statement that returns multiple column data
Case study : Query and SMITH All employees in the same department and position , Not included SMITH I am

(4) stay from Clause using subqueries
- Subquery statements appear in from clause . Here we need to use the technique of data query , Use a subquery as a temporary table .
- Get the average salary of each department , Think of it as a temporary table

② Find the name of the highest paid person in each department 、 Wages 、 department 、 Maximum wage

③ Display information for each department ( Department name , Number , Address ) And the number of people
- Method 1: Use multiple tables

- Method 2: Use subquery

(5) Merge query
- in application , In order to merge multiple select The results of the implementation of , You can use the set operator union,union all
①union This operator is used to get the union of two result sets . When using this operator , Duplicate lines in the result set will be automatically removed .
- Pay more than 2500 Or position is MANAGER Find out

②union all This operator is used to get the union of two result sets . When using this operator , Duplicate rows in the result set are not removed .
- Pay more than 25000 Or position is MANAGER Find out

③ The information columns must be the same , Otherwise there will be problems

(6) Summary
- Subqueries can appear in two places ( routine , important )
- 1. where In words , Use as a filter
- 2. from In words , Used to do Cartesian product with a specific table
边栏推荐
猜你喜欢
随机推荐
Library management system
New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version
Implementation principle of golang synergy
Etcd implementation of large-scale service governance application practice
Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
自组织是管理者和成员的双向奔赴
FTP协议详解
第五天实验
PHP lucky draw system with background source code
HTB-Blocky
【报错】node:internal/modules/cjs/loader:936 【解决方法】
数据截断及估计
Thirty years of MPEG audio coding
C语言:小乐乐与进制转换
Interpretation of ue4.25 slate source code
idea替换所有文件中的内容
Youxuan software appoints Huang Zhijun as the general manager of the company
Analysis of Project-based Learning Creativity in steam Education
这个博主,qt归类比较全,有空去学习总结,记录一下。
Really time NTP service startup command









