当前位置:网站首页>Zero foundation self-study SQL course | union joint query
Zero foundation self-study SQL course | union joint query
2022-06-11 07:41:00 【Meow Ningyi】
Hello everyone , I'm Ning Yi .
Today's talk SQL Course No 13 course :UNION The joint query .
In the first two classes JOIN Connect , Horizontal column consolidation can be performed on multiple tables .
This lesson is about UNION The operator , Is to combine multiple query results , Merge vertically by row .
Basic grammar :
SELECT < Field name > FROM < Table name >
UNION
SELECT < Field name > FROM < Table name >
1、 Single table joint query
UNION When merging result sets , If there are duplicate rows in the merged result set , Only one line will be retained .
example : Query out Students In the table , Number students Sid by 2 To 5 And 1 To 3 Your record passed UNION unite .
Instance analysis : UNION The result set above has Sid by 1、2、3 The record of ,UNION The following result sets have Sid by 2、3、4、5 The record of .UNION United time , Only duplicate lines will be retained ( namely Sid by 2、3 The record of ) One of the lines .
SELECT * FROM Students
WHERE Sid BETWEEN 2 AND 5
UNION
SELECT * FROM Students
WHERE Sid BETWEEN 1 AND 3

If you want to keep all duplicate lines , have access to UNION All Merge result set .
SELECT * FROM Students
WHERE Sid BETWEEN 2 AND 5
UNION ALL
SELECT * FROM Students
WHERE Sid BETWEEN 1 AND 3

2、 Joint query of different tables
UNION The number of columns in the upper and lower result sets of the operator must be equal , Otherwise, an error will be reported .
Column name of the final consolidated table , Determined by the result set above .
example : take Students In the table , Birthday Sage stay 1995 Names of students born before Sname And Teachers Name of teacher in the table Tname Merge .
SELECT Sname FROM Students
WHERE Sage < '1995-01-01'
UNION
SELECT Tname FROM Teachers

3、 Statement execution order
If it's followed by ORDER BY、LIMIT Clause , These clauses do not apply only to the last query statement , But will wait UNION Merge the result sets before executing .
SELECT * FROM Students
WHERE Sid BETWEEN 2 AND 5
UNION
SELECT * FROM Students
WHERE Sid BETWEEN 1 AND 3
ORDER BY Sid

ORDER BY After the result set is merged , And then sort the whole result set , Not just for the last query statement .
LIMIT Clause will also wait for the result set to be merged , Take the previous records .
SELECT * FROM Students
WHERE Sid BETWEEN 2 AND 5
UNION
SELECT * FROM Students
WHERE Sid BETWEEN 1 AND 3
LIMIT 2

Next class , Let's talk about aggregate functions .
Click to follow , Update the course and notify the first time ~
边栏推荐
- [atcoder2376] black and white tree (game)
- A case in which the MySQL administrator password cannot take effect
- Seata的几种事务模式
- Black Qunhui dsm7.0.1 physical machine installation tutorial
- 【Oracle 数据库】奶妈式教程day04 排序查询
- [STL source code analysis] summary notes (10): hashtable exploration
- Classes and objects (medium)
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- C language inherits memory management mechanism (unfinished)
- gaussDB for redis和 redis的区别?
猜你喜欢

If you want to save an IP address, what data type is better? 99% of people will answer wrong!

零基础自学SQL课程 | OUTER JOIN外连接

Qstring to hexadecimal qstring
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction
![[atcoder1980] mystious light (mathematical simulation)](/img/c0/7de31b36e11ff71328d927c1d1c2d3.png)
[atcoder1980] mystious light (mathematical simulation)

Use of wordcloud

Several transaction modes of Seata

【AtCoder1980】Mysterious Light(数学模拟)
随机推荐
零基础自学SQL课程 | OUTER JOIN外连接
黑群晖DSM7.0.1物理机安装教程
C language function stack frame
[STL source code analysis] summary note (2): overview of containers
Nosqlzoo question brushing-1
Sdl-4 PCM playback
What exactly is PMP?
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
Qstring to hexadecimal qstring
QT picture adaptive display control
【IoT】智能硬件:如何获取硬件产品的wifi信号强度
nosqlzoo刷题-1
C language three chess games
[analysis of STL source code] summary notes (3): vector introduction
Seata的几种事务模式
Find the longest common substring of N arrays (strings)
【AtCoder1980】Mysterious Light(数学模拟)
Compound ratemodel contract analysis
20200803 T3 my friends [divide and conquer NTT optimization recursion]
Nim product