gpt4 book ai didi

mysql - 查询在 MySQL 5.1 中有效,但在 SQL2000 中无效

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:08 25 4
gpt4 key购买 nike

我正试图从运行在 Win2000 Server SQL Server 2000 v8.00.760 上的旧数据库中提取一些数据,但出现错误。我的查询在我正在使用的当前数据库中工作,但我不知道我在做什么,如果那是问题所在,那不是向后可比的。

这是我的查询:

SELECT o.CustomerID, o.ShipName, o.ShipCity, o.ShipStateOrProvince, o.ShipPostalCode, o.ShipPhoneNumber, c.Profession, SUM( o.Total ) 
FROM Orders o

JOIN Customers c ON o.CustomerID = c.CustomerID

WHERE (o.OrderDate >= '2012-01-01')
AND (o.Void <> - 1)
AND (o.Cancelled <> - 1)

GROUP BY o.CustomerID

在 MySQL 中它工作正常。我正在按照我想要的方式返回查询。但是我得到了错误... enter image description here

当我在较旧的 SQL 版本中尝试时。

我现在有点迷路了。谁能告诉我我做错了什么?

谢谢!

最佳答案

SQL Server 对 GROUP BY 的要求比 MySQL 严格得多。 SELECT 子句中的所有非聚合列也必须出现在 GROUP BY 中。这避免了在例如给定 CustomerID 有多个 ShipName 值时可能发生的歧义。 MySQL 会任意选择一个,而 SQL Server 会强制您将它们视为单独的行。

SELECT o.CustomerID, o.ShipName, o.ShipCity, o.ShipStateOrProvince, o.ShipPostalCode, o.ShipPhoneNumber, c.Profession, SUM( o.Total ) 
FROM Orders o

JOIN Customers c ON o.CustomerID = c.CustomerID

WHERE (o.OrderDate >= '2012-01-01')
AND (o.Void <> - 1)
AND (o.Cancelled <> - 1)

GROUP BY o.CustomerID, o.ShipName, o.ShipCity, o.ShipStateOrProvince, o.ShipPostalCode, o.ShipPhoneNumber, c.Profession

关于mysql - 查询在 MySQL 5.1 中有效,但在 SQL2000 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179100/

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