Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql递归查询不返回父级_Mysql_Sql - Fatal编程技术网

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
的表中,这似乎是多余的。