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