我有3张桌子:
Buses:
id | bus_name
1 | 2D
2 | 1D
Routes:
id | route_name
1 | Garden
2 | Malir
Bus_Route
id......|....bus_id...|....route_id
1.......|......1......|....1
2.......|......1......|....2
3.......|......2......|....1
我想要一辆从'Garden'和'Malir'出来的公共汽车,即2D。我试过并且我成功地映射了一条路线,如果公共汽车只从花园通过但是有一个条件是有两个组合框一个是从公共汽车开始的地方和第二个结束的地方..我想要从公共汽车经过的整个路线像花园和malir作为1D我在上面的例子中提到的只是从Malir而不是从花园传递,所以如果Start是来自花园而且结束是在malir上,那么只有2D会显示结果..我的查询在下面:
select b.bus_name
from buses b, route r, bus_route br
where (b.id = br.bus_id AND r.id = br.route_id)
AND (r.id=1 AND r.id=2)
提供空结果集
想要修复它。
答案 0 :(得分:2)
SELECT bus_name
FROM buses
WHERE id in
(SELECT b.bid
FROM bus_route b
JOIN bus_route _b
ON b.bid = _b.bid
WHERE b.rid = 1 AND _b.rid = 2)
答案 1 :(得分:0)
您应该可以使用以下查询:
select *
from buses b
left join bus_route br
on b.id = br.bus_id
where br.route_id = 1
and exists (select *
from buses b1
left join bus_route br2
on b1.id = br2.bus_id
where br2.route_id=2
and b.id = b1.id)
返回结果:
| ID | BUS_NAME | BUS_ID | ROUTE_ID |
-------------------------------------
| 1 | 2D | 1 | 1 |