SELECT t.id, apa.district AS district
FROM tbl_activity AS t
JOIN tbl_activity_package AS ap ON ap.activity_id = t.id
JOIN tbl_activity_package_address AS apa ON apa.activity_package_id = ap.id
GROUP BY t.id
ORDER BY apa.district
查询后,输出为:
我想按地区返回一行。我怎么能在查询中做到这一点?
答案 0 :(得分:1)
您希望按district
分组,而不是id
分组。
但是,这会让您陷入困境:您希望为Evora区显示哪个id
值?你想要117 id,还是118 id?你将如何确定它?您需要一个特定的规则来满足您的需求。作为一个例子,这将给你更大的:
SELECT MAX(t.id) id, apa.district AS district
FROM tbl_activity AS t
JOIN tbl_activity_package AS ap ON ap.activity_id = t.id
JOIN tbl_activity_package_address AS apa ON apa.activity_package_id = ap.id
GROUP BY apa.district
ORDER BY apa.district
将MAX()更改为MIN()将为您提供较低的ID。您也可以拥有更复杂的条件,但这些条件需要首先将其构建为可以加入的派生表(子查询),以将您的条件映射到特定的ID号。