gpt4 book ai didi

mysql - 选择所有没有子行的行

转载 作者:IT老高 更新时间:2023-10-29 00:15:30 25 4
gpt4 key购买 nike

我真的在这个问题上脑筋急转弯。

我有下表:

id    parentID     name1     0            Frank2     1            Sally3     1            John4     3            Beth

I'd like a statement that selects only those items that have no children, so in the previous example:

Sally
Beth

会是结果。如果不创建递归函数,似乎无法找出执行此操作的查询,如果可以避免,我不想这样做。

最佳答案

select yt.name
from YourTable yt
where not exists (select null from YourTable where parentID = yt.id)

虽然效率较低(参见:Left outer join vs NOT EXISTS),但您也可以使用左连接来做到这一点:

select yt1.name
from YourTable yt1
left join YourTable yt2
on yt1.id = yt2.parentID
where yt2.id is null

关于mysql - 选择所有没有子行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5020032/

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