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排序时,注意两个结果的别名保持一致,使用别名排序很方便。当然也可以使用列数。