mysql join包含加入conditon的属性

时间:2016-10-06 20:00:51

标签: mysql sql performance join

让我有三个表评论,附件,descendant_comments

  1. 评论有很多评论,即自我加入
  2. 评论有很多附件
  3. descendent_comments包含评论的层次结构(父子,大子关系)
  4. 代码:

    SELECT comments.*, count(attachments.id)
    FROM comments com
    LEFT JOIN descendant_comments dc 
        ON dc.comments_id = com.id
    LEFT OUTER JOIN attachments atch 
        ON (atch.comment_id = dc.child_comment_id OR atch.comment_id = com.id)
    WHERE com.id = 77
    

    我不想使用OR,因为它不是最佳方式。

    有任何方法可以替换这行“OR atch.comment_id = com.id”

0 个答案:

没有答案