在查询中发现 sql_mode=only_full_group_by 错误。我尝试了很多方法来修复它。更改了 sql_mode=''
。但我仍然收到此错误。
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'intranet.a.tagid' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by:
我已经给出了表格字段
SELECT a.tagid,b.tagword,a.metric
from nextag_metrics a
LEFT JOIN nextag_words b ON a.tagid=b.id
WHERE b.tagword is not NULL
AND type='filedepot'
OR groupid in (2931,2938,2941)
OR userid in (1)
OR categoryid in (2,3,4,5,6,8,9,10,11)
GROUP BY b.tagword
ORDER BY b.tagword ASC
最佳答案
这是您的查询:
SELECT m.tagid, w.tagword, m.metric
FROM nextag_metrics m LEFT JOIN
nextag_words w
ON m.tagid = w.id
WHERE w.tagword is not NULL AND m.type = 'filedepot' OR
m.groupid in (2931, 2938, 2941) OR
m.userid in (1) OR
m.categoryid in (2,3,4,5,6,8,9,10,11)
GROUP BY w.tagword
ORDER BY w.tagword ASC;
此查询还存在其他问题。首先,对于您的特定问题,不要破坏数据库(只有完整的分组依据才是数据库应该如何工作)。修复查询。
以下是一些其他问题和潜在问题:
WHERE
子句可疑。我猜它并不完全符合您的要求。- 在大多数情况下,在左连接中聚合第二个表是愚蠢的。该值可能为
NULL
。 - 尚不清楚
tagid
和metric
应来自哪一行。
我建议您问另一个问题。提供示例数据(作为查询中的文本表)并清楚地解释您要执行的操作。
关于php - 查询语句中sql_mode=only_full_group_by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43588031/