gpt4 book ai didi

MySQL - 数据查询树?

转载 作者:行者123 更新时间:2023-11-29 05:05:23 25 4
gpt4 key购买 nike

我在 MySQL 数据库中有一个这样的数据集,但大约有 50,000 多条记录。

A01                   Description
A01.01 Description
A01.02 Description
A01.03 Description
A01.03.01 Description
A01.03.02 Description
A01.03.02.01 Description
A01.03.03 Description
A02 ....
A02.01
A03
B01
B02
B02.02
B02.03
...

我想做一个查询,我可以传递类似“A”的东西,它让我得到所有的 child ,但只有一层深,所以我得到 A01、A02、A03,但我还需要知道 A01和 A02 有 child ,而 A03 没有。

我需要重复传递“A01.03”的查询以获取 A01.03 的子项并知道 A01.03.02 有子项但其他没有。

我有一个包含大约 50,000 多个这样的数据库,需要按需高效地查询它。如果需要,我可能会稍微更改结构或添加标志。

任何提示或想法将不胜感激!谢谢!

最佳答案

如果我没理解错的话,你可以这样做:

select substring_index(t.col1, '.', 1),
( count(*) > 1 ) as has_children
from t
where t.col1 like 'A%'
group by substring_index(t.col1, '.', 1);

关于MySQL - 数据查询树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com