2010-05-28 62 views
0

我正在嘗試計算所有與前兩個表相關的表3的IDS,但我認爲我的SQL代碼是錯誤的,有人能幫我修復它嗎?MySQL彙總錯誤

下面是代碼:

$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 
+0

你知道,你可以發佈你得到的任何錯誤消息。或者如果你沒有收到錯誤,你可以發佈你得到的輸出和預期的輸出。 – 2010-05-28 06:43:13

回答

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 
0

您要使用的聚合函數,COUNT(),沒有GROUP第一荷蘭國際集團的結果。此外,您需要使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 

有關集合函數的和不明確的列名在查詢中的詳細信息,請查看MySQL參考手冊Group By FunctionsIndentifier Qualifiers