【问题标题】:What is the best query for this?对此最好的查询是什么?
【发布时间】:2013-08-01 22:49:08
【问题描述】:

我有表 bus, street, route_going, route_return

在我的餐桌街

例子:

id | name
1  | street1 
2  | street2
3  | street4
...
n  | streetn

table_route_going,我有例子:

id_bus | id_street | order
101    | 1         | 1
101    | 2         | 2
101    | 5         | 3

...

table 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)

所以,我认为这个查询不好。有人可以帮我说出这个搜索的“最佳”查询吗?

【问题讨论】:

  • 我们可以有一个SQL Fiddle 吗?请?此外,添加有关索引的信息。这可能会有所作为。
  • 我不明白,你的代码做的事情太多了,只是检查公共汽车是否穿过街道
  • @NoIdeaForName 穿过两条条街道!按顺序!
  • 我认为真正使问题复杂化的部分是订购要求。如果问题很简单,“它穿过这两条街道吗?”它会简单得多。如果去和返回不使用相同的数字进行订购也会更简单。 (如,如果返回值总是更高。)我最初的想法是将两个路线表合并到一个表中,并添加一列来指示公共汽车是否正在/返回,并在该表中使用数字将完全整理路线中的街道。

标签: mysql sql database select exists


【解决方案1】:
SET @x = 1;
SET @y = 5;

SELECT x.id_bus
  FROM   
     ( SELECT *,0 returning FROM route_going UNION SELECT *, 1 FROM route_return ) x
  JOIN
     ( SELECT *,0 returning FROM route_going UNION SELECT *, 1 FROM route_return ) y
    ON y.id_bus = x.id_bus
   AND (y.returning > x.returning OR (y.returning = x.returning AND y.porder > x.porder))
 WHERE x.id_street = @x
   AND y.id_street = @y;

【讨论】:

  • 如果您想要 DISTINCT 结果,请使用 DISTINCT 运算符。
  • 我想知道UNION ALL 是否会带来任何性能提升。因为这在UNION 中指定了01,所以不会有相同的行,因此检查重复行(如果引擎执行它)是浪费循环。
【解决方案2】:
select 
street.name,
route_going.id_bus,route_going.id_street
route_return.id_bus,route_return.id_street
on street.id=route_going.id_street and 
route_return.id_street=route_going.id_street;  

试试这个。谢谢

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多