sqlunionorderby
① sql union
在最后加个排序就好了
你想依据那个字段进行排序,或是说依据那个字段的条件将它排到最后一行,就再最后加上
order by 字段
② sql中union 和order咋在一起用
你写的好麻烦
你把这个语句单独运行一下
多运行几次,看每次的结果是不是你想要的那样
然后套到你的存储过程里
有问题追问
selectt.单词,t.翻译,t.等级
from
(select单词,翻译,等级,row_number()over(partitionby等级orderbynewid())rnfrom单词)
wherern=1
③ SQL语句中UNION排序问题
代码改写如下:
select a.输出字段1, a.输出字段2, a.输出字段3, ...a.输出字段n from
(select * ,1 as px from 表A where 软件名称 like '%迅雷%'
union
select * ,2 from 表A where 软件简介 like '%迅雷%') a order by a.px
如果不在意多出一个用于排序的字段“px”的话,代码可简化如下
select * ,1 as px from 表A where 软件名称 like '%迅雷%' order by 1
union
select * ,2 from 表A where 软件简介 like '%迅雷%'
***注意,因人为增加了一个排序用数字字段(第一个查询用1,第二个用2),UNION关键字的删除两个查询之间重复数据的功能会不起作用,如果需要保持删除重复记录的能力,则需要使用DISTINC关键字,例如:
select distinct a.输出字段1, a.输出字段2, a.输出字段3, ...a.输出字段n from
(select * ,1 as px from 表A where 软件名称 like '%迅雷%'
union
select * ,2 from 表A where 软件简介 like '%迅雷%') a order by a.px
上机试一试吧
④ 请教: SQL中的union结果优先排序问题
加入 NEWS 表里有 名字,年龄,生日 那么:
select * from
(
select 1 as 顺序 ,名字,年龄,生日 from news where title like '%亲人%'
union all
select 2 as 顺序,名字,年龄,生日 from news where title like '%朋友%'
) a
order by 顺序 -- 想家其他字段 就 再加上
⑤ SQL用了Union后的排序问题
select * from
(
select userid from userTable where userid in (201,202)
Union
select userid from userTable where userid in (101,102,301,302)
) as a
order by userid
⑥ SQL查询union...
select num,value from (
SELECT 1 as num,COUNT(*) as value FROM Class t1
union
SELECT 2,COUNT(*) FROM Gym_Usage t2
union
SELECT 3,COUNT(*) FROM Badminton_Usage t3
union
SELECT 4,COUNT(*) FROM Pool_Usage t4
) a
order by a.num
这样就搞定了顺序问题。
⑦ 各位大神,怎么在sql语句union中使用order by
试试把order by 提出来。
(select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Proct P, Customer C
where U.status = R.statusid
and U.ProctName = P.ProctName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待维修'
union
select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Proct P, Customer C
where U.status = R.statusid
and U.ProctName = P.ProctName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待检测')
order by u.RepairDate desc
⑧ 两条sql语句,union +排序
order by 放里面
select StartTime,EndTime from (select * from table order by startTime asc ) where endtime>getdate()
union
select StartTime,EndTime from (select * from table order by startTime asc ) where endtime<getdate()
⑨ sql union all 和order by 的问题请高手帮助
select row_number() over(order by Id) rn, Id,name from A
union all
select row_number() over(order by Id desc) rn,Id,name from B
显示的时候,rn 栏位不读取即可。希望你满意。
⑩ SQL中union 和order by的问题。。。
select c.* from
(select price,kg from a union select price,kg from b) c
order by c.price desc