【问题标题】:CodeIgniter 3 - nonaggregated column group byCodeIgniter 3 - 非聚合列分组
【发布时间】:2018-02-26 16:22:31
【问题描述】:

当我尝试在 CodeIgniter 3 中运行这个查询时,它给了我这个错误:

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'forum.phrases.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

SELECT `id`, `keyword`, `value`, `language` FROM `phrases` GROUP BY `language`

PHP:

$query = $this->db->select("id, keyword, value, language")
            ->group_by("language")
            ->get("phrases")
            ->result();

我google了一下,但我不太明白答案,主要是因为查询与CI无关并且非常复杂......如何在codeigniter中解决这个问题?

我不想更改任何 MySQL 设置。

【问题讨论】:

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

表达式#1

SELECT list is not in GROUP BY clause and contains nonaggregated column 'spd.quantity' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

只需在查询的上方添加以下行。

$this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
【解决方案2】:

这不是 CodeIgniter 特有的问题——它是 SQL 标准的一部分并且内置于 MySQL 中。它的存在是为了验证您的数据并鼓励良好的数据库设计和查询。您有两种选择来修复它:

  1. 您可以以“正确”的方式编写查询。这是 SQL92 规范的一个问题,但后来在 SQL99 标准中进行了修改以允许非聚合:https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

  2. 另一种方法,更常见的响应是修改您的my.cnf 文件并禁用only_full_group_by 的强制SQL 模式。 Disable ONLY_FULL_GROUP_BY

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多