我有一张表bus_stops
,其中包含巴士站的路线 ID,如下所示:
sl_no route_id stop_name
-------------------------------------------------------------
1 1234 stop1
2 1234 stop2
3 1235 stop7
4 1235 stop8
5 5678 stop1
6 5678 stop2
我想要一个包含 stop1 和 stop2 的 route_id(对于 stop1 作为 From station 和 stop2 作为 To station 我想获得 1234,但不是对于“stop2 as From Staion”和“stop1 as To Station”)。
我写了这个查询:
SELECT DISTINCT route_id FROM bus_stops AS myAlias
WHERE EXISTS ( SELECT * FROM bus_stops WHERE route_id = myAlias.route_id AND stops = 'stop1' )
AND EXISTS ( SELECT * FROM bus_stops WHERE route_id = myAlias.route_id AND stops = 'stop2' )
但它返回 route_id 为“stop1 作为 From station 和 stop2 作为 To station”和“stop2 as From Staion and stop1 as To Station”