0

我正在尝试计算与前两个表相关的 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
4

2 回答 2

0

您正在尝试使用聚合函数 ,COUNT()而没有GROUP先获取结果。此外,您需要在与Adrian的查询COUNT()类似的不太模糊的范围内创建“id”列。请尝试以下查询:

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

有关查询中聚合函数和不明确列名的更多详细信息,请查看 MySQL Reference Manual for Group By Functions and Indentifier Qualifiers

于 2010-05-28T07:00:34.933 回答
0

你可以试试这个吗?

SELECT COUNT(table3.id) as num
  FROM table3, table2, table1
WHERE table1.id = table2.id 
  AND table2.id = table3.id
  AND table2.id = $id
于 2010-05-28T06:47:04.193 回答