ID Name FatherID Birthyear

1   Bart    NULL     1756
2   Franz   1        1796
3   Josef   2        1835
4   Zohan   3        1887

假设我有这张表,我想知道佐汉是巴特的儿子,如果我把“FatherID”列的值与前面几行的ID进行比较,直到我找到巴特,就可以得到这个表。但是如何比较同一个表中不同行和列的值

最佳答案

您可以自行加入表格:

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

10-06 00:42