UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果.
union
union
union all 比union 速度快很多,建议在相同的情况下用 union all
注意:union用法中,两个select语句的字段类型匹配,而且字段个数要相同。
例:
1、建两表并向其中插入示例数据:
create
insert into TABLE_A(A,B) values ('1','A');
insert into TABLE_A(A,B) values ('2','A');
insert into TABLE_A(A,B) values ('3','B');
insert into TABLE_A(A,B) values ('4','B');
select * from TABLE_A
create
insert into TABLE_B (A,B) values ('2','A');
insert into TABLE_B (A,B) values('3','A');
insert into TABLE_B (A,B) values('4','A');
select * from TABLE_B;
2、要求:查询两张表中B相等的且B='A'的记录。
3、分析:结果应该是五条记录。
4、测试:



5、结果:union all 正确
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22181866/viewspace-1115864/ ,如需转载,请注明出处,否则将追究法律责任。