【发布时间】:2018-08-14 13:23:24
【问题描述】:
我一直坚持让所有孩子的查询父 ID 大于客户 ID
表格测试
id name parent
1 test1 0
2 test2 1
3 test3 1
4 test4 2
5 test5 2
6 test6 10
7 test7 10
8 test8 6
9 test9 6
10 test10 5
11 test10 7
目前我正在使用这个递归查询,但它显示孩子直到 10 父级,但无法提供 6 岁和 7 岁及更多的孩子
SELECT id , parent FROM (SELECT id , parent from (SELECT * FROM test order by
parent , id) testdata_sorted, (SELECT @pv := '1') initialisation where
find_in_set(parent , @pv) > 0 and @pv := concat(@pv, ',', id) ORDER BY
parent ASC) AS tt
当前输出为 ->
id parent
2 1
3 1
4 2
5 2
10 5
6 10
7 10
我需要这种类型的输出。我需要这方面的帮助。
id parent
2 1
3 1
4 2
5 2
10 5
6 10
7 10
8 6
9 6
11 7
【问题讨论】:
-
您能否提供您的架构,以便我们查看相关的各种表?
-
如果您需要父 id 大于 id 的记录,它不应该显示 id=2 的记录,因为它的父是 1 较小。 (还有更多可以找到)
-
您不想切换到嵌套集吗?
-
您使用什么版本的 MySQL(或 MariaDB)?这会有所不同,因为 MySQL 8+ 和 MariaDB 10.2+ 具有递归公用表表达式。 (查找这些。)
-
@SupaMonkey 你能告诉我怎么可能吗?
标签: php mysql mysqli parent-child hierarchy