我有公交车、街道、去程、回程的表
在餐桌街我有
示例:
id | name
1 | street1
2 | street2
3 | street4
...
n | streetn
表route_going,我有示例:
id_bus | id_street | order
101 | 1 | 1
101 | 2 | 2
101 | 5 | 3
...
表route_return,我有例子:
id_bus | id_street | order
101 | 3 | 1
101 | 2 | 2
101 | 1 | 3
...
好的,在这个例子中,公交车101从街道1,2和5按这个顺序行驶,公交车从街道3,2和1按这个顺序返回。
我想知道哪些公交车经过街道“x”和街道“y”(先经过x,后经过y)
例如:
x = 1, y = 5 -> the bus 101 pass
x = 1, y = 3 -> the bus 101 pass
x = 3, y = 1 -> the bus 101 pass
x = 3, y = 5 -> the bus 101 don't pass
因此,我用于发现公交车的sql是...(例如,1号和5号街道的过路情况)
select * from bus as b where
-- The bus passes between the 2 streets at the going route??
exists (select * from route_going as rg1, route_going as rg2,street as r1,street as r2 where rg1.id_bus = rg2.id_bus and rg1.id_street = r1.id and rg2.id_street = r2.id and r1.id = 1 and r2.id = 5 and b.bus_id = rg1.id_bus and rg1.order <= rg2.order)
-- The bus passes between the 2 streets at the return route??
or exists (select * from route_return as rg1, route_return as rg2,street as r1,street as r2 where rg1.id_bus = rg2.id_bus and rg1.id_street = r1.id and rg2.id_street = r2.id and r1.id = 1 and r2.id = 5 and b.bus_id = rg1.id_bus and rg1.order <= rg2.order)
-- The bus passes between the 2 streets at the going route first and return route later??
or exists (select * from route_going as rg1, route_return as rg2,street as r1,street as r2 where rg1.id_bus = rg2.id_bus and rg1.id_street = r1.id and rg2.id_street = r2.id and r1.id = 1 and r2.id = 5 and b.bus_id = rg1.id_bus)
所以,我认为这个查询不好。谁能帮我说一下这个搜索的“最好”查询是什么?
2条答案
按热度按时间z9ju0rcb1#
t9aqgxwy2#
试试这个.谢谢