Union
Basic Sentence
1 | SELECT column_name(s) FROM table_name1 |
- The SELECT statements within a UNION must have the same number of columns. Columns must also have similar data types. Meanwhile, the order of the columns in each SELECT statement must be the same.
- SELECT will automatic deduplication, while SELECT ALL will not.
UNION
employees:
emp_id | name | dpt_id |
---|---|---|
1 | apple | 101 |
2 | boy | 102 |
3 | cat | 101 |
4 | dog | 103 |
former_employees:
emp_id | name | dpt_id |
---|---|---|
5 | egg | 102 |
2 | boy | 102 |
1 | SELECT name, dpt_id FROM employees |
result:
name | dpt_id | |
---|---|---|
apple | 101 | |
boy | 102 | automatic deduplication |
cat | 101 | |
dog | 103 | |
egg | 102 |
UNION ALL
1 | SELECT name, dpt_id FROM employees |
result:
name | dpt_id | |
---|---|---|
apple | 101 | |
boy | 102 | first record |
cat | 101 | |
dog | 103 | |
egg | 102 | |
boy | 102 | from former_employees |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Yuki-I-Rain!