zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    计算机网络
    计算机网络知识总结
    final,static,super,this
    ArrayList
    基础面试题
    BATJ都爱问的多线程面试题
    Session过期、失效时间
    类加载器ClassLoader源码解析
    连接ftp服务器 JDK 1.7
    根据当前请求的特征,判断该请求是否来自手机终端
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10773929.html
Copyright © 2011-2022 走看看