使用分组/计数:
SELECT t.bus_line_id,
t.bus_line_type,
t.bus_line_number
FROM BUS_LINE t
JOIN LINK_BUS_NEIGBOURHOOD lbn ON lbn.bus_line_id = t.bus_line_id
JOIN LINK_NEIGHBOURHOOD_STREET lns ON lns.neighbourhood_id_ns = lbn.neighbourhood_id_ns
JOIN STREET s ON s.street_id = lns.street_id
WHERE s.street_id IN (12, 14)
GROUP BY t.bus_line_id,
t.bus_line_type,
t.bus_line_number,
s.street_id
HAVING COUNT(DISTINCT s.street_id) = 2
使用 JOIN:
SELECT t.bus_line_id,
t.bus_line_type,
t.bus_line_number
FROM BUS_LINE t
JOIN LINK_BUS_NEIGBOURHOOD lbn ON lbn.bus_line_id = t.bus_line_id
JOIN LINK_NEIGHBOURHOOD_STREET lns ON lns.neighbourhood_id_ns = lbn.neighbourhood_id_ns
JOIN STREET s ON s.street_id = lns.street_id
AND s.steet_id = 12
JOIN STREET s2 ON s2.street_id = lns.street_id
AND s2.steet_id = 14