日期:2014-05-19  浏览次数:20454 次

UNION 查询时 为何不是第一个表的数据排在前面 在线等
select       a1,a2,a3       from       a          
  union              
  select       b1,b2,b3       from       b          

查询结果   是   a表中的数据和b表中的数据混合着   ,没有指定任何排序,有什么办法指定   结果是   a表中的数据排在前面,表b中的数据库排在后面

------解决方案--------------------
select *
from (
select a1,a2,a3,1 as xh from a
union
select b1,b2,b3,2 as xh from b ) T
order by xh
------解决方案--------------------
select *
from (
select a1,a2,a3,1 as xh from a
union
select b1,b2,b3,2 as xh from b ) T
order by xh
这种方法可行!
------解决方案--------------------
ls正解。
------解决方案--------------------
select a1,a2,a3 ,1 as id from a
union
select b1,b2,b3,2 from b
order by id

------解决方案--------------------
--可以合并重复行。

select *
from (
select a1 as f1,a2 as f2,a3 as f3,1 as xh from a
union
select b1 as f1,b2 as f2,b3 as f3,2 as xh from b) T1
where checksum(f1,f2,f3) in
(select checksum(*)
from
(
select a1 as f1,a2 as f2,a3 as f3 from a
union
select b1 as f1,b2 as f2,b3 as f3 from b
) as T2)
order by xh