select id,name,price from t2
如果用union all 然后再排序,出来的结果集顺序就不对了
因为查询量大,所以,我觉得用临时表插入数据也不行的,因为那样的话临时表数据操作太频繁了,我认为会有问题的,不知道我想的对不对
不知道各位大侠有没有什么好的解决方案
5 个解决方案
#1
--try it
select id,name,price from (
select id,name,price,1 tmp from t1
union all
select id,name,price,2 tmp from t2 ) t
order by tmp,price
select id,name,price from (
select id,name,price,1 tmp from t1
union all
select id,name,price,2 tmp from t2 ) t
order by tmp,price
#2
--不知楼主是否是这个意思。第一个在前,按照price排序,第二个在后,按照price排序
select id,name,price,'1' as col1 from t1
union all
select id,name,price,'2' from t2
order by col1,price
select id,name,price,'1' as col1 from t1
union all
select id,name,price,'2' from t2
order by col1,price
#3
select *
from (
select top 100 percent id,name,price
from t1 order by price
)t
union all
select id,name,price from t2
from (
select top 100 percent id,name,price
from t1 order by price
)t
union all
select id,name,price from t2
#4
--生成测试数据
create table #t1(id int,name char(1),price int)
insert into #t1 select 1,'a',100
insert into #t1 select 2,'b',120
insert into #t1 select 3,'c',110
insert into #t1 select 4,'d',160
insert into #t1 select 5,'e',150
create table #t2(id int,name char(1),price int)
insert into #t2 select 6,'f',80
insert into #t2 select 7,'g',20
insert into #t2 select 8,'h',10
insert into #t2 select 9,'i',60
insert into #t2 select 10,'j',50
--执行查询
select
a.id,a.name,a.price
from
(select id,name,price,1 as nid from #t1
union all
select id,name,price,2 as nid from #t2) a
order by
a.nid,(case a.nid when 1 then a.price else 1 end)
--输出结果
/*
id name price
---- ---- -----
1 a 100
3 c 110
2 b 120
5 e 150
4 d 160
6 f 80
7 g 20
8 h 10
9 i 60
10 j 50
*/
--删除测试数据
drop table #t1,#t2
create table #t1(id int,name char(1),price int)
insert into #t1 select 1,'a',100
insert into #t1 select 2,'b',120
insert into #t1 select 3,'c',110
insert into #t1 select 4,'d',160
insert into #t1 select 5,'e',150
create table #t2(id int,name char(1),price int)
insert into #t2 select 6,'f',80
insert into #t2 select 7,'g',20
insert into #t2 select 8,'h',10
insert into #t2 select 9,'i',60
insert into #t2 select 10,'j',50
--执行查询
select
a.id,a.name,a.price
from
(select id,name,price,1 as nid from #t1
union all
select id,name,price,2 as nid from #t2) a
order by
a.nid,(case a.nid when 1 then a.price else 1 end)
--输出结果
/*
id name price
---- ---- -----
1 a 100
3 c 110
2 b 120
5 e 150
4 d 160
6 f 80
7 g 20
8 h 10
9 i 60
10 j 50
*/
--删除测试数据
drop table #t1,#t2
#5
marking...
#1
--try it
select id,name,price from (
select id,name,price,1 tmp from t1
union all
select id,name,price,2 tmp from t2 ) t
order by tmp,price
select id,name,price from (
select id,name,price,1 tmp from t1
union all
select id,name,price,2 tmp from t2 ) t
order by tmp,price
#2
--不知楼主是否是这个意思。第一个在前,按照price排序,第二个在后,按照price排序
select id,name,price,'1' as col1 from t1
union all
select id,name,price,'2' from t2
order by col1,price
select id,name,price,'1' as col1 from t1
union all
select id,name,price,'2' from t2
order by col1,price
#3
select *
from (
select top 100 percent id,name,price
from t1 order by price
)t
union all
select id,name,price from t2
from (
select top 100 percent id,name,price
from t1 order by price
)t
union all
select id,name,price from t2
#4
--生成测试数据
create table #t1(id int,name char(1),price int)
insert into #t1 select 1,'a',100
insert into #t1 select 2,'b',120
insert into #t1 select 3,'c',110
insert into #t1 select 4,'d',160
insert into #t1 select 5,'e',150
create table #t2(id int,name char(1),price int)
insert into #t2 select 6,'f',80
insert into #t2 select 7,'g',20
insert into #t2 select 8,'h',10
insert into #t2 select 9,'i',60
insert into #t2 select 10,'j',50
--执行查询
select
a.id,a.name,a.price
from
(select id,name,price,1 as nid from #t1
union all
select id,name,price,2 as nid from #t2) a
order by
a.nid,(case a.nid when 1 then a.price else 1 end)
--输出结果
/*
id name price
---- ---- -----
1 a 100
3 c 110
2 b 120
5 e 150
4 d 160
6 f 80
7 g 20
8 h 10
9 i 60
10 j 50
*/
--删除测试数据
drop table #t1,#t2
create table #t1(id int,name char(1),price int)
insert into #t1 select 1,'a',100
insert into #t1 select 2,'b',120
insert into #t1 select 3,'c',110
insert into #t1 select 4,'d',160
insert into #t1 select 5,'e',150
create table #t2(id int,name char(1),price int)
insert into #t2 select 6,'f',80
insert into #t2 select 7,'g',20
insert into #t2 select 8,'h',10
insert into #t2 select 9,'i',60
insert into #t2 select 10,'j',50
--执行查询
select
a.id,a.name,a.price
from
(select id,name,price,1 as nid from #t1
union all
select id,name,price,2 as nid from #t2) a
order by
a.nid,(case a.nid when 1 then a.price else 1 end)
--输出结果
/*
id name price
---- ---- -----
1 a 100
3 c 110
2 b 120
5 e 150
4 d 160
6 f 80
7 g 20
8 h 10
9 i 60
10 j 50
*/
--删除测试数据
drop table #t1,#t2
#5
marking...