2012-12-14 59 views
1

我的查詢在下面(它搜索父母和子女上下4個級別)。因此,我得到了在單個「num」值中找到的行數:約22k行。MySQL:當使用GROUP BY時,count(id)返回1而不是實際總行數

SELECT COUNT(fts_trip_id) AS num 
FROM feed_trips_se 
JOIN departures ON d_departure_id=fts_trip_departure_id 
JOIN destinations ON destinations.d_destination_id=d_departure_id 
LEFT JOIN destinations AS destChild1 ON destChild1.d_destination_parent_id=destinations.d_destination_id 
LEFT JOIN destinations AS destChild2 ON destChild2.d_destination_parent_id=destChild1.d_destination_id 
LEFT JOIN destinations AS destChild3 ON destChild3.d_destination_parent_id=destChild2.d_destination_id 
LEFT JOIN destinations AS destChild4 ON destChild4.d_destination_parent_id=destChild3.d_destination_id 
LEFT JOIN destinations AS destParent1 ON destParent1.d_destination_id=destinations.d_destination_parent_id 
LEFT JOIN destinations AS destParent2 ON destParent2.d_destination_id=destParent1.d_destination_parent_id 
LEFT JOIN destinations AS destParent3 ON destParent3.d_destination_id=destParent2.d_destination_parent_id 
LEFT JOIN destinations AS destParent4 ON destParent4.d_destination_id=destParent3.d_destination_parent_id 
WHERE fts_trip_date>=NOW() 
ORDER BY fts_trip_date ASC, fts_trip_price ASC 

然而,當我使用大量的LEFT JOIN的,數量〜22K不可車次正確的金額。我需要通過實際的fts_trip_id對這些行進行分組。

不幸的是,當我執行GROUP BY時,我得到的行數與正確的行程數(〜8k)相同,而不是正確的行數。所有的行然後顯示一個數字,通常是1,但也是10或13.

我真的不知道爲什麼當我使用GROUP BY函數時,我沒有得到〜8k的單行值。誰能幫我?

回答

7

您可以使用COUNT(DISTINCT):​​3210來計算ID。 GROUP BY將只計算每個ID的結果數量。

+0

工作很好,謝謝! –