SQL高级过滤

时间:2015-04-17 06:22:20

标签: mysql sql set

我有一个名为route_location的表,如下表所示:

enter image description here

此表适用于地图。有路线,每条路线都有一个唯一的routeID。路线包含位置,任何路线中出现的位置顺序由orderID定义。我想获取所有路线中的所有位置,这些路线位于特定locationID之后(在我目前的情况下为11),该信息由orderID提供。

我想从orderID超过给定值的orderID的任何路径获取所有locationID。 我想要orderID大于locationID = 11的orderID的所有位置。

目前的答案是

locationID=12,13,16  

[12,13 from routeID=1,

 16 from routeID=2, 

no data from routeID=3]

我该怎么办?我认为这与这篇文章有关: Advanced filter in SQL

非常感谢你!

更新

  

locationID的orderID必须大于路径

中locationID = 11的orderID

3 个答案:

答案 0 :(得分:4)

目前还不清楚,但您似乎要求应用于订单集的逻辑:在之后返回所有数据在之前的同一路线上有locationID = 11 orderID

SELECT *
FROM route_location AS rl
WHERE EXISTS
 ( SELECT * 
   FROM route_location AS rl2
   WHERE rl.routeID = rl2.routeID -- on the same route
     AND rl.orderID < rl2.orderID -- any previous order
     AND rl2.locationID = 11      -- had a locationID of 11
 )

答案 1 :(得分:0)

试试这个

    SELECT *
    FROM route_location AS rl
    inner join route_location AS r2
    on rl.routeID = r2.routeID and  rl.orderID < r2.orderID and r2.locationID = 11

答案 2 :(得分:0)

select * from route_location r1
where orderid > (select orderid from route_location r2
                 where locationId = 11
                   and r1.routeID = r2.routeID)