如何比较来自不同列和不同行但相同 table 的值?
How do I compare values from different column and different row but same table?
ID Name FatherID Birthyear
1 Bart NULL 1756
2 Franz 1 1796
3 Josef 2 1835
4 Zohan 3 1887
假设我有这个 table,我想知道 Zohan 是否是 Bart 的儿子,如果我将列 "FatherID" 中的值与前几行直到我到达巴特。但是我如何比较相同 table 但不同行和列的值
您可以自行加入 table:
SELECT s.name AS son_name, f.name AS father_name
FROM mytable s
JOIN mytable f ON s.fatherID = f.id
-- possibly add a where clause with conditions on son/father names
ID Name FatherID Birthyear
1 Bart NULL 1756
2 Franz 1 1796
3 Josef 2 1835
4 Zohan 3 1887
假设我有这个 table,我想知道 Zohan 是否是 Bart 的儿子,如果我将列 "FatherID" 中的值与前几行直到我到达巴特。但是我如何比较相同 table 但不同行和列的值
您可以自行加入 table:
SELECT s.name AS son_name, f.name AS father_name
FROM mytable s
JOIN mytable f ON s.fatherID = f.id
-- possibly add a where clause with conditions on son/father names