Mysql 获取同一表中多个对象的所有父对象
对于批量编辑功能,我需要加载多个对象的父对象。 使用单个查询执行此操作将杀死数据库。我使用的是MySQL 5.7,我的表是这样构建的:Mysql 获取同一表中多个对象的所有父对象,mysql,sql,hierarchical-data,recursive-query,mysql-5.7,Mysql,Sql,Hierarchical Data,Recursive Query,Mysql 5.7,对于批量编辑功能,我需要加载多个对象的父对象。 使用单个查询执行此操作将杀死数据库。我使用的是MySQL 5.7,我的表是这样构建的: CREATE TABLE `testtable` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `content` text, `parentid` bigint(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAUL
CREATE TABLE `testtable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`content` text,
`parentid` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
INSERT INTO testtable(`id`, `content`, `parentid`)
VALUES(1, 'parentA1', 0),(5, 'parentA2', 1),(3, 'childA', 2),
(4, 'parentB', 0),(5, 'childB', 4);
id | content | parentid | searchingChildID
1 | parentA1| 0 | 3
2 | parentA2| 1 | 3
3 | childA | 2 | 3
4 | parentB | 0 | 5
5 | childB | 4 | 5
对于单对象查询,我使用此语句获取所有父对象:
SELECT t.id, t.content, @pid := t.parentid AS parentid
FROM (SELECT * FROM Table1 order by id DESC) t
JOIN (SELECT @pid := 3) tmp
WHERE t.id = @pid
但我完全不知道,如果不使用并集,这怎么可能同时适用于多个对象
我的惠斯特输出应如下所示:
CREATE TABLE `testtable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`content` text,
`parentid` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
INSERT INTO testtable(`id`, `content`, `parentid`)
VALUES(1, 'parentA1', 0),(5, 'parentA2', 1),(3, 'childA', 2),
(4, 'parentB', 0),(5, 'childB', 4);
id | content | parentid | searchingChildID
1 | parentA1| 0 | 3
2 | parentA2| 1 | 3
3 | childA | 2 | 3
4 | parentB | 0 | 5
5 | childB | 4 | 5
提前谢谢 如果您运行的是MySQL 8.0,这是递归查询的典型用例。假设您想要对象3和4的所有父对象,可以执行以下操作:
with recursive cte as (
select t.id as originalid, t.* from table1 where id in (3, 4)
union all
select c.originalid, t.*
from cte c
inner join table1 t on t.id = c.parentid
)
select * from cte
对于批量编辑功能,我需要加载多个对象的父对象
对于这次行动,我希望:
id | content | parentid | ultimateparent
1 | parentA1| 0 | 0
2 | parentA2| 1 | 0
3 | childA | 2 | 0
4 | parentB | 0 | 0
5 | childB | 4 | 0
因为0是所有行的最终父级。如果您知道最大深度,可以在MySQL的8之前版本中使用左连接:
select t1.*, coalesce(t3.parentid, t2.parentid, t1.parentid) as ultimateparent
from testtable t1 left join
testtable t2
on t2.id = t1.parentid left join
testtable t3
on t3.id = t2.parentid
您可以通过更多的左连接来扩展它,以处理更深层次的亲子关系
如果0真的意味着没有父项,这在NULL可用时是一个奇怪的选择,那么您似乎想要:
id | content | parentid | ultimateparent
1 | parentA1| 0 | 1
2 | parentA2| 1 | 1
3 | childA | 2 | 1
4 | parentB | 0 | 4
5 | childB | 4 | 4
然后,您实际上可以将上述查询调整为:
select t1.*, coalesce(t3.id, t2.id, t1.id) as ultimateparent
from testtable t1 left join
testtable t2
on t2.id = t1.parentid left join
testtable t3
on t3.id = t2.parentid;
他是一把小提琴
您还可以为每个父级获取最大子级,尽管这似乎是任意的。在MySQL 8.0之前的版本中,变量是最简单的方法:
select t1.*,
(@max_child := if(@up = ultimateparent, @max_child,
if(@up := ultimateparent, id, id)
)
) as max_childid
from (select t1.*, coalesce(t3.id, t2.id, t1.id) as ultimateparent
from testtable t1 left join
testtable t2
on t2.id = t1.parentid left join
testtable t3
on t3.id = t2.parentid
order by ultimateparent, id desc
) t1 cross join
(select @up := -1, @max_child := -1) params;
你的MySQL版本是什么?提供完整的表脚本创建表+插入并严格显示所提供数据的所需输出。我不理解searchingChildID是如何确定的。你能解释一下吗?