SQL 高级过滤

SQL advanced filtering

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

这个table是一张地图。有路线,每条路线都有唯一的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]

我该怎么做?我认为这与post有关: Advanced filter in SQL

非常感谢您!

更新:

The orderID of the locationID must be greater than the orderID of locationID=11 in the route

试试这个

    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
select * from route_location r1
where orderid > (select orderid from route_location r2
                 where locationId = 11
                   and r1.routeID = r2.routeID)

目前还不清楚,但您似乎要求将逻辑应用于订单集:return 所有数据 after there was a locationID = 11 on the与之前 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
 )