【发布时间】:2014-07-13 19:20:32
【问题描述】:
我有理由确定这是一个愚蠢的问题,但我用谷歌搜索了 Stackoverflow,但仍然无法弄清楚我做错了什么。
我有以下数据库架构:
指南
id
first_name
last_name
guides_trips
id
guide_id
trip_id
旅行
id
name
我正在尝试获取指南列表,其中包括他们注册的所有旅行的计数,最终目标是能够按旅行标准过滤此计数(因此是第二次加入)。到目前为止,这是我所得到的:
SELECT
guides.id,
guides.first_name,
guides.last_name,
trips.name,
guides_trips.trip_id,
guides_trips.guide_id
FROM guides
LEFT JOIN guides_trips ON guides.id = guides_trips.guide_id
LEFT JOIN trips ON guides_trips.trip_id = trips.id
结果如下
id first_name last_name name trip_id guide_id
---------- ---------- ---------- --------------- ---------- ----------
1 Karelle Ritchie Mt. Erie Custom 2 1
1 Karelle Ritchie Everest Base Ca 7 1
1 Karelle Ritchie Mt. Baker 11 1
1 Karelle Ritchie Washington Pass 28 1
1 Karelle Ritchie Shuksan Custom 31 1
1 Karelle Ritchie Shuksan 40 1
1 Karelle Ritchie Shuksan 50 1
1 Karelle Ritchie Mt. Baker Custo 70 1
1 Karelle Ritchie Shuksan 73 1
2 Genesis Hoppe Rainy Pass 13 2
2 Genesis Hoppe Rainy Pass Cust 33 2
2 Genesis Hoppe Shuksan 48 2
2 Genesis Hoppe Rainy Pass 60 2
3 Yolanda Ziemann Washington Pass 15 3
3 Yolanda Ziemann Washington Pass 19 3
这是我尝试开始工作的原因
SELECT
guides.id,
guides.first_name,
guides.last_name,
trips.name,
guides_trips.trip_id,
guides_trips.guide_id,
COUNT(trips.id) AS trips_count
FROM guides
LEFT JOIN guides_trips ON guides.id = guides_trips.guide_id
LEFT JOIN trips ON guides_trips.trip_id = trips.id
GROUP_BY guides.id;
很遗憾,这给了我一个Error: near "GROUP_BY": syntax error
我做错了什么?
【问题讨论】:
-
GROUP BY中没有下划线 -
...当你希望你有一只橡皮鸭的时候。非常感谢,我最近显然写了太多 ActiveRecord 查询!
-
很高兴“敲定了这个案子”(对不起)。睡得好! :)
标签: sql join sqlite inner-join jointable