我有一张有以下列的表:

+-----------+--------------+-----------+
|PrevStation|CurrentStation|NextStation|
+-----------+--------------+-----------+
|-          |A             |B          |
+-----------+--------------+-----------+
|A          |B             |C          |
+-----------+--------------+-----------+
|B          |C             |D          |
+-----------+--------------+-----------+
|C          |D             |E          |
+-----------+--------------+-----------+
|D          |F             |-          |
+-----------+--------------+-----------+

现在我想通过以下方式表示一个视图:
+---------------+
|ClosestStations|
+---------------+
|A              |
+---------------+
|B              |
+---------------+
|C              |
+---------------+
|D              |
+---------------+
|E              |
+---------------+
|F              |
+---------------+

*注意:我希望排序的行具有第一个表的协定*
你觉得解决办法怎么样?

最佳答案

试试这个

Select [CLoseStation] from
(
   select PrevStation AS [CLoseStation],PrevStation ,NextStation,CurrentStation
   From T
   Union
   Select NextStation AS [CLoseStation],PrevStation ,NextStation,CurrentStation
   From T
   union
   Select CurrentStation AS [CLoseStation],PrevStation ,NextStation,CurrentStation
   From T
) AS S Order By PrevStation ,NextStation,CurrentStation ASC

08-04 12:55