中易网

各位大神,怎么在sql语句union中使用order by?

答案:3  悬赏:20  
解决时间 2021-04-21 16:56
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
使这个语句执行通就行
最佳答案
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
全部回答
--我举个例子你就懂了 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总和进行排序
试试把order by 提出来
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
鲜奶炒滑蛋怎么做
算命说的桃花受远不受近是什么意思
荆松一级公路正式通车没有
中国移动金肯学院江宁校区动感地带品牌店这个
想咨询一下小户型多功能沙发床到底好不好用的
有没有校园招聘进北京或天津摩比斯的同学?普
为什么人被蚊子要到以后会起包?
国庆相关知识问题
自动售水机价格
学英语专业去江西南部航空工业大学怎么样?自
宝宝发热贴退烧贴后额头红红的要怎样办
22周岁,上牙齿拥挤不齐,矫正需要几年
手机电池前三次充电可不可以边玩边充啊?这样
谁能给我10来个音速种子?
求自动给汉字加拼音的软件, 还有office2007
推荐资讯
配套7.5千瓦的柴油机是多大马力的?
9月有什么大片上映?
海上田园至罗湖末班车是几点
收视率平均2%点几左右算不算高?
我以前精神很好 现在在自己怎么一点精神也也
国产机能安装FM收音机吗?
日用品店起名字
我的脚很臭,怎么才可以让他不臭
财源建材门市地址在什么地方,想过去办事
梦见自己和陌生男子结婚
求南宁专业十字绣裱框材料店,南宁没有,广州
c1科目二得上课内容
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?