27,579
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select *,1 as tbidx from SQL1
union all
select *,2 as tbidx from SQL2
) tt order by tbidx,fuid
select * from (select * from table1 order by id) a
union all
select * from (select * from table2 order by id) b
union和union all 关键字都是将两个结果集合合并为一个,但这两者从使用和效率上来说都有所不同。
UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。实际大部分应用中是不会产生重复的记录,最常见的是过程表与历史表UNION。如:
select * from 表
union
select * from 表
select * from
(select 1 as f1,* from productt where fuid in (select id from classtp where jibie <>1 and id <>144)
union
select 2,* from productt where fuid in(
select id from classtp
where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)
) as a
order by f1,fuid,sortid desc
--不好意思,应该是这样
select * from (select top 10000000000 * from table1 order by id,...) a
union all
select * from (select top 10000000000 * from table2 order by id,...) b
select top 10000000000 * from table1 order by id,...
union all
select top 10000000000 * from table2 order by id,...
SELECT *
FROM
(
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144)-- order by fuid,sortid desc union
select * from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)-- order by fuid,sortid desc
) AS A
order by fuid,sortid desc
select * from
(
select *,1 as tbidx from SQL1
union all
select *,2 as tbidx from SQL2
) tt order by tbidx,fuid
select * from
(select * ,'sql1' as 'sql' from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc
union
select * ,'sql2' as 'sql2' from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144) )
order by sql,fuid,sortid desc
select * from
(
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc union
select * from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)
) a
order by fuid,sortid desc
select * from
(
select * from SQL1
union all
select * from SQL2
) tt
order by fuid