1

我的查询如下(它搜索上下 4 个级别的父母和孩子)。因此,我得到了在单个“num”值中找到的行数:大约 22k 行。

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 的单行值。谁能帮我?

4

1 回答 1

7

您可以使用COUNT(DISTINCT) :COUNT(DISTINCT fts_trip_id)来获取 id 的计数。GROUP BY 只会计算每个 id 的结果数。

于 2012-12-14T15:07:52.620 回答