私はテーブルbus、street、route_going、route_returnを持っています
私が持っているテーブルストリートで
例:
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)
ですから、このクエリは良くないと思います。この検索に「最適な」クエリを教えてください。