当前位置:网站首页>MySQL composite query
MySQL composite query
2022-06-30 12:20:00 【Xiao Ni-_-】
List of articles
As we explained earlier mysql Table query is to query a table , In actual development, we often need to query multiple tables .
Basic query review
- Check that the salary is higher than 500 Or position is MANAGER Of employees , At the same time, their initials should be capitalized J

- Sort by department number in ascending order and employee's salary in descending order

- Use annual salary to sort in descending order

- Displays the name and position of the highest paid employee

- Displays information about employees whose wages are higher than the average

- Displays the average wage and maximum wage for each department

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

- Displays the total number of employees for each position , Average wage

Multi-table query
In actual development, data often comes from different tables , Need multi table query .
Case a : 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

Case 2 : The display department number is 10 The name of your department , Employee name and salary

Case three : Show the name of each employee , Wages , And salary level

Self join
Self join refers to joining queries in the same table
example :
Show employees FORD The number and name of the superior (mgr Is the employee leader's number –empno)
- Subquery used

- Use multi table query ( Self query )

Subquery
Subqueries are embedded in other sql Statement select sentence , Also called nested query
Single line sub query
A single row subquery is a subquery that returns one row of records
Example :
Show SMITH Employees in the same department

Multi line sub query
Multi row subquery is a subquery that returns multi row records
- 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

- 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 )

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
example : Query and SMITH All employees in the same department and position , Not included SMITH I am

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 .
Case study :
- Displays the name of each employee whose salary is higher than the average salary of his / her department 、 department 、 Wages 、 Average wage

- 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
- Use multiple tables

- Use subquery

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 .
Case study : 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 .
Case study : Pay more than 25000 Or position is MANAGER Find out

边栏推荐
- 使用深度学习进行生物网络分析
- Redis6 learning notes - Chapter 2 - Basic redis6 operations
- Map collection
- Typescript readonlyarray (read only array type) details
- Use of polarplot function in MATLAB
- 剑指 Offer 05. 替换空格: 把字符串 s 中的每个空格替换成“%20“
- [cloud native | kubernetes] in depth understanding of deployment (VIII)
- 光谱共焦位移传感器的原理是什么?能应用那些领域?
- 8253 counter introduction
- A review of quantum neural networks 2022 for generating learning tasks
猜你喜欢
随机推荐
Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
Map集合
【BUG解决】fiftyone报AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘错误解决方法
[cf] 803 div2 B. Rising Sand
Getting started with the go language is simple: go handles XML files
Embedded SIG | 多 OS 混合部署框架
List集合
Time function and clock_ Differences between gettime() functions
会议预告 | 华为 2012 实验室全球软件技术峰会-欧洲分会场
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
Remove invalid parentheses [simulate stack with array]
Pinda general permission system (day 7~day 8)
海思3559 sample解析:venc
Use of redis in projects
Typescript readonlyarray (read only array type) details
SuperMap 3D SDKs_Unity插件开发——连接数据服务进行SQL查询
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
海思3559开发常识储备:相关名词全解
iServer发布ES服务查询设置最大返回数量
ES6新特性介绍
![移除无效的括号[用数组模拟栈]](/img/df/0a2ae5ae40adb833d52b2dddea291b.png)








