UNION 与 UNION ALL 常见用法

来源:互联网 发布:三菱plc仿真教学软件 编辑:程序博客网 时间:2024/05/16 10:27
select country from websites union select country from apps;--连接两个表的查询结果集,重复的不显示select country from websites union all select country from apps order by country;--连接俩个个表的查询结果集,显示重复select country,name from websites where country = 'CN' union all select country,app_name from apps where country='CN' order by name; --通过where条件查询的结果,连接连个表的结果集,并根据名字排序。

使用UNION命令时需要注意,只能在最后使用一个ORDER BY命令,是将两个查询结果合在一起之后,再进行排序!绝对不能写两个ORDER BY命令。

另外,在使用ORDER BY排序时,注意两个结果的别名保持一致,使用别名排序很方便。当然也可以使用列数。

原创粉丝点击