sql - SQL历史遍历表
问题描述
假设我有一张桌子
Current History Result
-------------------------
5 6 black
6 4 White
4 1 Black
使用数字 5,我如何在历史中回溯 2 次以得出结果是黑色还是白色?
5 -> 6 -> 4 -> black
假设表中还有其他数据行。
我努力了:
select *
from t.result
where t.current in t.history
and t.current in t.history where t.current in ('5')
解决方案
如果要求达到 2 级,您可以尝试以下逻辑 -
SELECT A.[Current],A.History,C.[Current],C.Result
FROM your_table A
INNER JOIN your_table B ON A.History = B.[Current]
INNER JOIN your_table C ON B.History = C.[Current]
WHERE A.[Current] IN (5)