union联合
的结果集不会有重复值,如果要有重复值,则使用union all
union会自动压缩多个结果集合中重复的结果,使结果不会有重复行,union all 会将所有的结果共全部显示出来,不管是不是重复。
union:会对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序。
union all:对两个结果集进行并集操作,包括重复行,不会对结果进行排序。
1.sql union用法 select 字段1 from 表名1 union select 字段2 from 表名2; 2.sql union all用法 select 字段1 from 表名1 union all select 字段2 from 表名2;
employee_china表:
employee_usa表:
union:
select e_id,e_name from employees_china union select e_id,e_name from employees_usa
去掉:
union all:
select e_id,e_name from employees_china union all select e_id,e_name from employees_usa
不能去掉:
当存在不相同的字段时: (employees_china 增加一个字段 e_hello)
必须进行补充:
select e_id,e_name,e_hello from employees_china union all select e_id,e_name,' ' as e_hello from employees_usa
到此这篇关于sql语句中union的用法小结的文章就介绍到这了,更多相关sql union内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论