获取有序的记录子集

Getting ordered subset of records

我有 table,它基本上描述了一条公共汽车路线。 table 包括特定路线的所有站点。

This SQLFiddle 显示了一个例子。数据显示一个方向的流量,但路线也可以是另一个方向。

我需要根据起点站和终点站获取这条路线的一部分(或全部)。例如,我可能有这些:

A -> M (subset would be B -> L)
A -> K (subset would be B -> J)
C -> H (subset would be D -> G)
I -> M (subset would be J -> L)

但是,如果方向是 H -> B,则子集需要是 G -> C。

序列栏就是为了这个目的。

是否有仅使用 SQL 的干净简单的方法?

假设我理解你的问题,这似乎很简单。
要获得子集,只需在 sequence 列上使用 between...and 运算符:

SELECT stop_from, stop_to
FROM routes
WHERE sequence BETWEEN 2 AND 11
ORDER BY sequence

要得到相反方向的子集,按desc排序即可,select stop_tostop_fromstop_fromstop_to :

SELECT stop_to As stop_from, stop_from As stop_to
FROM routes
WHERE sequence BETWEEN 3 AND 7
ORDER BY sequence DESC

See fiddle here