我有以下代码:
select O.ONO
from Customers C, Odetails Od, Orders O, Parts P
where P.PNO=Od.PNO and Od.ONO=O.ONO and O.CNO=C.CNO
group by O.ONO
order by sum(Od.QTY*P.PRICE)desc;
但是,我需要从其他表中选择O.ONO
,而不是选择C.CNAME
列。我该怎么做呢?
答案 0 :(得分:1)
如果您想要的是按订单总和排序的客户名称列表,那么只需更改您的查询
select O.ONO
...
group by O.ONO
到
select C.CName
...
group by C.CName
这将显示客户名称和订单号。注意:这将重复客户名称,与该客户的订单重复次数
另外,我首选的SQL查询样式是
select C.CName
from Customers C
INNER JOIN Orders O ON C.CNO = O.CNO
INNER JOIN Odetails OD ON O.ONO = OD.ONO
INNER JOIN Parts P ON OID.PNO = P.PNO
where P.PNO=Od.PNO and Od.ONO=O.ONO and O.CNO=C.CNO
group by C.CName
order by sum(Od.QTY*P.PRICE)desc;
澄清您的代码的其他建议。 (请注意:我不是oracle人员),在我的公司(SqlServer)中,通常你会得到以实体命名的表,主键只有Id
,并且然后任何没有前缀的属性。例如,OID
是指OrderId
还是OfficeId
?
<强> EG:强>
客户:
订单:
然后您的查询更具可读性:
SELECT Name
FROM Customer
INNER JOIN Order ON Customer.Id = Order.CustomerId
WHERE ...
ORDER BY Customer.Name
答案 1 :(得分:0)
请这样做,
select C.CNAME from Customers C, Odetails Od, Orders O, Parts P where P.PNO=Od.PNO and Od.ONO=O.ONO and O.CNO=C.CNO group by C.CNAME order by sum(Od.QTY*P.PRICE) desc;
答案 2 :(得分:0)
select语句将选择指定的列
SELECT C.CNAME
FROM Customers C, Odetails Od, Orders O, Parts P
WHERE P.PNO=Od.PNO and Od.ONO=O.ONO and O.CNO=C.CNO
GROUP BY C.CNAME
ORDER BY sum(Od.QTY*P.PRICE)desc;
如果您仍希望选择O.ONO列
SELECT C.CNAME, O.ONO
FROM Customers C, Odetails Od, Orders O, Parts P
WHERE P.PNO=Od.PNO and Od.ONO=O.ONO and O.CNO=C.CNO
GROUP BY C.CNAME
ORDER BY sum(Od.QTY*P.PRICE)desc;