php - 查询语句中sql_mode=only_full_group_by

标签 php mysql sql

在查询中发现 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:

我已经给出了表格字段

table1

table2

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
  • 尚不清楚 tagidmetric 应来自哪一行。

我建议您问另一个问题。提供示例数据(作为查询中的文本表)并清楚地解释您要执行的操作。

关于php - 查询语句中sql_mode=only_full_group_by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43588031/

相关文章:

php - 返回一个带有可变参数列表的方法作为 PHP 中的引用

php - 如何避免JS代码变成input?

php - 无法使用sql删除表中的数据

mysql - 一种在语言标记后清理特定字符串的方法

php如何从exe文件接收多个cout?

php - 当窗口不在焦点上时停止的 Javascript 倒数计时器

php - Youtube Analytics API 人口统计维度 "gender"显示在分析仪表板上,但在 API Explorer 上给出的查询无效

php - Joomla 虚拟数据导入

php - 获取刚刚插入的行的 ID

sql - 对象不支持此属性或方法 : 'rsSQL.Execute'