Mysql递归查询不返回父级
我创建了一个嵌套的注释系统,如下所示:Mysql递归查询不返回父级,mysql,sql,Mysql,Sql,我创建了一个嵌套的注释系统,如下所示: id user_id comment comment_date parent_comment_id 我试图通过这些父ID获取一条评论线索。我的问题是: with recursive cte (id, user_id, comment, comment_date, parent_comment_id) as ( select id, user_id, comment,
id
user_id
comment
comment_date
parent_comment_id
我试图通过这些父ID获取一条评论线索。我的问题是:
with recursive cte (id, user_id, comment, comment_date, parent_comment_id) as (
select id,
user_id,
comment,
comment_id,
parent_comment_id
from comments
where parent_comment_id = 'MES-738fc5be20b24b57978b3e873237ef12'
union all
select c.id,
c.user_id,
c.comment,
c.comment_date,
c.comment_parent_id
from comments c
inner join cte
on c.parent_comment_id = cte.id
)
select * from cte
除了不返回第一个父注释(其父注释id为null)外,其他注释均有效。我可能做错了什么?我猜你只是想要一个不同的锚:
select id, user_id, comment, comment_id, parent_comment_id
from comments
where id = 'MES-738fc5be20b24b57978b3e873237ef12'
可能,您需要
其中id='MES-738fc5be20b24b5797b3e873237ef12'
而不是其中parent_comment_id='MES-738fc5be20b24b5797b3e873237ef12'
它已经说了parent_comment_id='MES-738fc5be20b24b57979b378b3e12'
@devidix。样本数据将有助于可视化正在发生的事情。例如,id
和comment\u id
之间有什么区别?在名为comments
的表中,这似乎是多余的。