+--------------+---------------+
| First number | Second number |
+--------------+---------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
+--------------+---------------+
所以我在SQL上有这个表。我想得到一个特定数字的所有后代。例如,如果我选择数字2,结果应为:
+--------------+---------------+
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
+--------------+---------------+
数字不需要按照正确的顺序排列:
+--------------+------------------+
| First number | Following number |
+--------------+------------------+
| 2 | 3 |
| 4 | 5 |
| 3 | 4 |
| 1 | 2 |
+--------------+------------------+
我应该继续获得之前的结果。
我不知道该怎么做,我希望你能帮助我。
答案 0 :(得分:1)
您可以使用Recursive CTE
WITH RECURSIVE t
AS (SELECT *
FROM yourtable
WHERE "First number" = 2
UNION ALL
SELECT a.*
FROM yourtable a
JOIN t b
ON b."Second number" = a."First number")
SELECT *
FROM t
答案 1 :(得分:0)
这可以通过简单的select语句来实现。
select * from table where "first number" >= 2