create table test_tree(
t_id varchar(100),
parent_id varchar(100)
);
insert into test_tree values('a', '');
insert into test_tree values('b', 'a');
insert into test_tree values('c', 'b');
insert into test_tree values('d', 'c');
insert into test_tree values('e', 'd');
insert into test_tree values('f', 'c');
insert into test_tree values('1', '');
insert into test_tree values('2', '1');
insert into test_tree values('3', '2');
insert into test_tree values('4', '3');
insert into test_tree values('5', '4');
insert into test_tree values('6', '3');
查询语句1(字符串,没有结果)
SELECT t_id FROM
(
SELECT * FROM test_tree where parent_id <> ''
) realname_sorted,
(SELECT @pv :='c') initialisation
WHERE (FIND_IN_SET(parent_id, @pv)<> '' And @pv := concat(@pv, ',', t_id));
查询语句2(数字,有结果)
SELECT t_id FROM
(
SELECT * FROM test_tree where parent_id <> ''
) realname_sorted,
(SELECT @pv :='3') initialisation
WHERE (FIND_IN_SET(parent_id, @pv)<> '' And @pv := concat(@pv, ',', t_id));
参考的这篇文章:https://ostack.cn/a/11...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…