两张表,comments表和reply表。一个是评论一个是回复,reply中的targetId 对应着 comments表中的commentId,也就是说一条回复对应一条评论。
我想把comments表的所有数据(所有被评论)查出来,如果对应的一条评论有回复的话就塞进去,没有回复就为null,
但是我查出来结果总是只有被回复了的评论才能查出来,不是所有的。
sql 语句
select sql_calc_found_rows c.content, c.nickName, r.replyContent, r.replyTime from
comments as c left outer join reply as r on c.commentsId=r.targetId and 1=1
where c.isDelete=0 and c.articleId=1 and r.isDelete=0
如图
我希望查出来的结果
实际查出来的结果
select sql_calc_found_rows c.content, c.nickName, r.replyContent, r.replyTime from
comments as c left outer join reply as r on c.commentsId=r.targetId and 1=1
where c.isDelete=0 and c.articleId=1 and (r.isDelete=0 or r.isDelete is null);