gpt4 book ai didi

php - 没有 GROUP BY 子句时 SQL 计数返回错误

转载 作者:可可西里 更新时间:2023-11-01 07:46:28 25 4
gpt4 key购买 nike

为什么我在实时服务器上执行此查询时出错,但在我的 wamp 本地主机上却没有?

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

我怎样才能解决这个问题?

我的本​​地主机返回这个结果,这是我需要的,

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选项打开。

关于php - 没有 GROUP BY 子句时 SQL 计数返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815760/

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