1.union和union all 都是实现数据库连表查询的. 但都需具备2张表的的列, 字段都一样.
2.union 实现连表查询的, 是不允许重复的, 重复值会被覆盖掉.
例如:
表一: person1
ID | name | age |
---|---|---|
1 | zhangsan | 23 |
– | – | – |
2 | lisi | 24 |
– | – | – |
3 | wangwu | 25 |
表二: person2
ID | name | age |
---|---|---|
1 | zhangsan | 23 |
– | – | – |
2 | zhouyi | 21 |
– | – | – |
3 | zhouer | 22 |
sql语句:
SELECT name FROM person1
union
SELECT name FROM person2;
查询结果:
name |
---|
zhangsan |
– |
lisi |
– |
wangwu |
– |
zhouyi |
– |
zhouer |
3.union all连表查询是可以重复的.
sql语句:
SELECT name FROM person1
union
SELECT name FROM person2;
查询结果:
name |
---|
zhangsan |
– |
lisi |
– |
wangwu |
– |
zhangsan |
– |
zhouyi |
– |
zhouer |
来源:CSDN
作者:weixin_45765705
链接:https://blog.csdn.net/weixin_45765705/article/details/104777991