为什么我在我的实时服务器上遇到此查询但在我的wamp localhost上没有错误?
SELECT
type as type,
COUNT(*) AS total
FROM page AS p
WHERE p.parent_id = p.page_id
错误信息是,
sqlSTATE[42000]: Syntax error or access violation: 1140 Mixing of
GROUP columns (MIN(),MAX(),COUNT(),…) with no GROUP columns is
illegal if there is no GROUP BY clause
我怎么能绕过这个?
我的localhost返回这个结果,这就是我需要的,
type total
page 16
解决方法:
使用聚合函数(如COUNT)时,需要包含GROUP BY
子句.
SELECT
type as type,
COUNT(*) AS total
FROM page AS p
WHERE p.parent_id = p.page_id
GROUP BY type
至于为什么这在本地工作,而不是在你的实时服务器上; MySql doesn’t require complete listing of non-aggregate columns in the GROUP BY
clause by default,但您的实时服务器可能已启用ONLY_FULL_GROUP_BY
选项.