article表如下:

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列
错误信息:

网友评论