sql 同一张表查询不同数据合并之后关联查询

SELECT
    t.articleId articleId,
    comments.`comments` parentComment,
    t.commentId commentsId,
    comments.`id` parentId,
    comments.`parent_name` parentName,
    t.userId userId,
    comments.`group_id` groupId,
    t.thisComment comments,
    comments.`prase_num` praseNum,
    comments.`create_time` createTime,
    comments.`update_time` updateTime,
    comments.`user_image` userImage,
    comments.`recommend`,
    comments.`user_type` userType,
    t.userName userName,
    article.`title`,
    article.`plain_txt` plainTxt,
    article.`coefficient_original` coefficientOriginal,
    article.`first_issue` firstIssue,
    article.`grade_id` gradeId,
    article.`author`,
    article.`status`,
    article.`share_status` shareStatus,
    article.`cover_sort` coverSort,
    article.`cover_img_url` coverImgUrl,
    article.`is_show` isShow,
    article.`timing_publish_time` timingPublishTime,
    article.`note`,
    article.`settop_type` settopType,
    article.`top_begin_time` topBeginTime,
    article.`top_end_time` topEndTime,
    article.`published_type` publishedType,
    article.`review_time` reviewTime,
    article.`submit_time` submitTime,
    bussinessAmount.id 'bussinessId',
    ifnull( bussinessAmount.reading_amount_in, 0 ) 'readingAmountIn',
    ifnull( bussinessAmount.reading_amount_out, 0 ) 'readingAmountOut',
    ifnull( bussinessAmount.comment_amount, 0 ) 'commentAmount' 
FROM
                            (
                            SELECT
                                nideshop_comments.`parent_id` parentId,
                                nideshop_comments.`id` commentId,
                                nideshop_comments.`article_id` articleId,
                                nideshop_comments.`comments` thisComment,
                                nideshop_comments.`user_id` userId,
                                nideshop_comments.`user_name` userName
                            FROM
                                nideshop_comments 
                            WHERE
                                1 = 1  
                                AND nideshop_comments.`user_id` =   544
                                AND nideshop_comments.`STATUS` = 1 
                        Union All 
                            SELECT
                                nideshop_comments.`parent_id` parentId,
                                nideshop_comments.`id` commentId,
                                nideshop_comments.`article_id` articleId,
                                nideshop_comments.`comments` thisComment,
                                nideshop_comments.`user_id` userId,
                                nideshop_comments.`user_name` userName
                            FROM
                                nideshop_comments 
                            WHERE
                                1 = 1  
                                AND nideshop_comments.`STATUS` = 1 
                                AND nideshop_comments.`parent_id` in ( 
                                                                                                            SELECT
                                                                                                                nideshop_comments.`id` commentId
                                                                                                            FROM
                                                                                                                nideshop_comments 
                                                                                                            WHERE
                                                                                                                1 = 1  
                                                                                                                AND nideshop_comments.`user_id` =   544
                                                                                                                AND nideshop_comments.`STATUS` = 1 
                                                                                                            )   
                                
                            ) t
    INNER JOIN nideshop_comments comments ON t.parentId = comments.`id`
    LEFT JOIN nideshop_article article ON article.id = t.articleId
    LEFT JOIN nideshop_article_bussiness_amount bussinessAmount ON article.id = bussinessAmount.article_id 
WHERE
    comments.`STATUS` = 1 
ORDER BY
    comments.create_time DESC

 

posted @ 2019-04-26 14:01  xiaoshen666  阅读(2625)  评论(0编辑  收藏  举报