合并结果集

2019-11-14  本文已影响0人  江河湖海琴瑟琵琶

article表如下:


1566804207(1).png
select * from article where id > 1; #结果三条记录,2,3,4
select * from article where id = 2; #结果一条记录,2

#union 和 union all 区别
#union会先把两个结果集合并,然后去重,再返回
select * from article where id > 1
union 
select * from article where id = 2; 
#结果3条记录,2,3,4

#union all 直接合并两个结果集,没有去重操作,速度快,但可能出现重复数据
select * from article where id > 1
union all
select * from article where id = 2;
#4结果条记录,2,3,4,2

注意:两个结果集必须有相同的列.否则会报错

select * from article where id > 2 #4列
union
select id from article where id = 2;#1列

错误信息:


1566806409(1).png
上一篇 下一篇

猜你喜欢

热点阅读