【问题标题】:MySQL aggregation errorMySQL聚合错误
【发布时间】:2010-05-28 06:40:15
【问题描述】:

我正在尝试计算与前两个表相关的 table3 中的所有 id,但我认为我的 SQL 代码有误,有人可以帮我修复它吗?

代码如下:

$dbc = mysqli_query($mysqli,"SELECT table1.*, table2.*, COUNT(id) as num, table3.id
                             FROM table1
                             INNER JOIN table2 ON table1.id = table2.id
                             INNER JOIN table3 ON table2.id = table3.id
                             WHERE table2.id = '$id'");

这是错误信息。

1140: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4

【问题讨论】:

  • 您知道,您可以发布收到的任何错误消息。或者,如果您没有收到错误,您可以发布您获得的输出和预期的输出。

标签: mysql sql mysql-error-1140


【解决方案1】:

你可以试试这个吗?

SELECT COUNT(table3.id) as num
  FROM table3, table2, table1
WHERE table1.id = table2.id 
  AND table2.id = table3.id
  AND table2.id = $id

【讨论】:

    【解决方案2】:

    您正在尝试使用聚合函数COUNT(),而不是先使用GROUPing 结果。此外,您需要使COUNT() 中的“id”列不那么模糊,类似于Adrian 的查询。请尝试以下查询:

    SELECT table1.*, table2.*, COUNT(table3.id) as num, table3.id
      FROM table3, table2, table1    
    WHERE table1.id = table2.id     
      AND table2.id = table3.id    
      AND table2.id = $id  
    GROUP BY
      table1.*,
      table2.*,
      table3.id
    

    有关查询中聚合函数和不明确列名的更多详细信息,请查看Group By FunctionsIndentifier Qualifiers 的 MySQL 参考手册。

    【讨论】:

      猜你喜欢
      • 2021-02-03
      • 2018-05-03
      • 1970-01-01
      • 2022-01-25
      • 2014-07-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多