我有一个MySQL表(系列),如下所示
id parent_id
4 null
5 4
6 null
7 null
36 5
37 6
这时我传递
id as 4 it should return 4 -5 - 36
id as 5 it should return 4 -5 - 36
id as 6 it should return 6 - 37
id as 7 it should return 7
id as 36 it should return 4 -5 - 36
id as 37 it should return 37 - 6
SELECT t1.id AS lev1, t2.id as lev2, t3.id as lev3, t4.id as lev4
FROM family AS t1
LEFT JOIN family AS t2 ON t2.parent_id = t1.id
LEFT JOIN family AS t3 ON t3.parent_id = t2.id
LEFT JOIN family AS t4 ON t4.parent_id = t3.id
WHERE t1.id =5;
但是这些代码在某些情况下失败了。有人可以帮我这个吗?
答案 0 :(得分:0)
根据提供的内容,我可以看到36和37不会返回您期望的值。
t1.id = 36
然后t2.parent_id应该= 36,但它们都没有。
此外,'失败'是不是很模糊是否有错误或者你得到的结果与你预期的不同?如果是后者,那么你可能会期待错误的结果。