• 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
  • 相关阅读:
    每日Linux命令不完整命令
    mysql安装
    自动调用杀毒软件对文件进行杀毒
    正则获得字符串数组,以字符串分隔获取
    利用SQL语句查找某数据库中所有存储过程包含的内容
    TextBox的滚动条自动到最底部、利用枚举获取HashTable中的值
    Ajax中如何使用Session变量,Cookies可以用表单验证的方式获取并使用。
    用SQL语句批量生成一个表的INSERT语句
    利用DataSet、DataTable、DataView按照自定义条件过滤数据
    向线程传递数据与线程用回调方法检索数据
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10773929.html
Copyright © 2020-2023  润新知