select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待维修'
order by U.RepairDate desc
union
select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待检测'
order by u.RepairDate desc
使这个语句执行通就行
各位大神,怎么在sql语句union中使用order by?
答案:3 悬赏:20
解决时间 2021-04-21 16:56
- 提问者网友:饮鸿
- 2021-04-20 16:35
最佳答案
- 二级知识专家网友:情战凌云蔡小葵
- 2021-04-20 18:11
select * from (
select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
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, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待检测'
) a
order by RepairDate desc
select u.*, r.statusname, p.*, c.*
from UserLogin U, RepairStatus R, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
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, Product P, Customer C
where U.status = R.statusid
and U.ProductName = P.ProductName
and U.BoatName = C.BoatName
and U.RepairDate <= '2011-05-25'
and R.statusname = '待检测'
) a
order by RepairDate desc
全部回答
- 1楼网友:承载所有颓废
- 2021-04-20 20:16
--我举个例子你就懂了
CREATE TABLE ts
(
id INT,
VALUE INT
)
GO
INSERT ts
SELECt 1,2 UNIOn ALL
SELECT 1,3 UNIOn ALL
SELECT 1,2 UNIOn ALL
SELECT 2,4 UNIOn ALL
SELECT 2,21 UNIOn ALL
SELECT 3,2 UNIOn ALL
SELECT 3,2
GO
SELECT id ,sum(value) AS s_v
FROM ts
GROUP BY id
ORDER BY sum(VALUE)
就是通过分组聚合后 对分组元素进行排序
本例中就是对ID分组 然后按照id对应的value总和进行排序
- 2楼网友:萌萌哒小可爱
- 2021-04-20 18:52
试试把order by 提出来
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯