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
  • 相关阅读:
    pythonsys.exit()
    Python字符串格式化
    Json概述以及python对json的相关操作
    python学习笔记——异常
    Python:sys.argv[]用法
    python学习笔记——字符串,列表,字典,集合,数值,sorted
    python class 的属性
    Python模块——unittest 单元测试
    从sql2000 复制数据到sql2005
    abstract、virtual及override
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10773929.html
Copyright © 2011-2022 走看看