union all 的语法如下:
[sql 语句 1]
union all
[sql 语句 2]
我们用和上一页同样的例子来显示出 union all 和 union 的不同
union all查询允许您结合的结果,设置2个或更多的“”查询。它返回的所有行(即使该行存在超过一个的“select”报表)。
联盟内的每个sql语句的所有查询必须在结果中相同的字段数套类似的数据类型。
语法一个union all查询:
代码如下 复制代码
select field1, field2, . field_n
from tables
union all
select field1, field2, . field_n
from tables;
实例
代码如下 复制代码
select supplier_id
from suppliers
union all
select supplier_id
from orders;
如果supplier_id出现在供应商和订单表,它会多次出现在你的结果集。union 查询不会删除重复。
下面看实例来过滤重复的
代码如下 复制代码
select supplier_id, supplier_name
from suppliers
where supplier_id > 2000
union all
select company_id, company_name
from companies
where company_id > 1000
order by 2;
这样就ko了哈。