永发信息网

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

答案:3  悬赏:60  手机版
解决时间 2021-11-14 07:07
各位大神,怎么在sql语句union中使用order by?
最佳答案
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 * ,提示为明确定义的列

数据库是oralce
我想这个应该不是问题,
sql语句该是一样的追答可能是你的几个表,列名有重复导致的,
比如UserLogin 表有RepairDate 字段,Product 表也有RepairDate 字段造成的
在内层的select不要用*,改用具体的列名
全部回答

--我举个例子你就懂了
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 提出来。
(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 = '待检测')
order by u.RepairDate desc
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
滚动轴承3202和5202有什么不同
一百八十分之八十乘以三点一四乘九。
水星4冲称9.9马里发动机多少钱
承包建筑工程一般谁来看工地
吃哪种花粉好
炮台新寨门东面在哪里
芳华缝纫机压脚怎么安装
雷达脉冲重复频率低会有什么坏处
义的繁体字拆开怎么解释?忠字拆开怎么解释?
不得抄袭,不得套作 什么意思?
学生长发什么卷发好看
有耐高温,硬度高,导热性差的材料吗
关于河北省丰宁满族自治县凤山镇的历史故事和
b∪zz的意思
悼念大姨夫去世三周年
推荐资讯
请问D26在哈市发车时会晚点吗???
电脑机箱和主板的线怎么连?
临汾临7中高考录取分
江门市崖门镇三村小学地址有知道的么?有点事
120克10袋脆枣,250克6袋脆枣,两个哪个算下来
尼龙六是什么意思
黄瓜怎么小黄瓜纽都化了
为什么九华山看不到地藏王菩萨的肉身
从德胜门怎样去八达岭长城啊?
C+HNO₃(浓)—CO₂+NO₂+H
主角总想抢我机缘txt
五个旧水电瓶能卖多少钱
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?