gpt4 book ai didi

mysql - 在一个查询中同时使用 GROUP BY 和 ORDER BY

转载 作者:行者123 更新时间:2023-11-29 01:51:52 25 4
gpt4 key购买 nike

有人告诉我不能在一个 MySQL 查询中使用 GROUP BY 和 ORDER BY。这是查询的缩写版本 -

SELECT n.colorName, n.colorComp, n.colorID, SUM(n.gallons) AS TotalGallons
FROM netTran n, Store m, Product p
WHERE ((n.store = m.store) and m.state = "FL")
AND ((n.salesNbr = p.salesNbr) AND (p.intExt = "EXTERIOR" OR p.intExt = "INT/EXT"))
AND ((n.clrnt1 = "L1") AND (n.clrnt1 = "R3"))
GROUP BY n.colorComp, n.colorID
ORDER BY TotalGallons DESC;

有人告诉我,将 ORDER BY 与 GROUP BY 结合使用会得到不同的结果,并且 ORDER BY 工作的唯一方法是将主查询嵌套在

SELECT * FROM
(query)
ORDER BY TotalGallons DESC;

对吗?

最佳答案


使用查询作为

SELECT n.colorName, n.colorComp, n.colorID, SUM(n.gallons) AS     TotalGallons
FROM netTran n, Store m, Product p
WHERE ((n.store = m.store) and m.state = "FL")
AND ((n.salesNbr = p.salesNbr) AND (p.intExt = "EXTERIOR" OR p.intExt = "INT/EXT"))
AND ((n.clrnt1 = "L1") AND (n.clrnt1 = "R3"))
GROUP BY n.colorName, n.colorComp,n.colorID
ORDER BY TotalGallons DESC;

您可以在单个查询中使用分组依据和排序依据。但是如果要聚合列,则需要提供所有列

关于mysql - 在一个查询中同时使用 GROUP BY 和 ORDER BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com