MySQL - 查询数据树?

MySQL - Query Tree of Data?

我在 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" 的东西,它让我得到所有 children 但只有一层深,所以我得到 A01、A02、A03,但我还需要知道 A01 和 A02 有 children 而 A03 没有。

我需要重复传递 "A01.03" 的查询以获得 A01.03 的 children 并且知道 A01.03.02 有 children 但其他没有。

我有一个包含大约 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);