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
sql 同一张表查询不同数据合并之后关联查询的更多相关文章
- sql语句-如何在SQL以一个表中的数据为条件据查询另一个表中的数据
select *from 表2where 姓名 in (select 姓名from 表1where 条件) 这个就是用一个表的查询结果当作条件去查询另一个表的数据
- SQL两张表筛选相同数据和不同数据
原文链接:http://www.cnblogs.com/onesmail/p/6148979.html 方法一: select distinct A.ID from A where A.ID not ...
- Sql server中如何将表A和表B的数据合并(乘积方式)
sql server中如何将表A 和表B的数据合并成乘积方式,也就是说表A有2条数据,表B有3条数据,乘积后有6条数据, 这就要用到sql中的笛卡尔积方式了 1.什么是笛卡尔积 笛卡尔积在SQL中的实 ...
- mysql结构相同的三张表查询一条记录\将一张表中的数据插入另外一张表
将一张表中的数据插入另外一张表 1.两张表结构相同 insert into 表1名称 select * from 表2名称 2.两张结构不相同的表 insert into 表1名称(列名1,列名2,列 ...
- SQLServer两张表筛选相同数据和不同数据
概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...
- sql之将一个表中的数据注入另一个表中
sql之将一个表中的数据注入另一个表中 需求:现有两张表t1,t2,现需要将t2的数据通过XZQHBM相同对应放入t1表中 t1: t2: 思路:left join 语句: select * from ...
- EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的
我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...
- MYSQL基础操作之数据约束与关联查询
一.MYSQL约束 1.默认值约束,当字段没有插入值的时候,mysql自动给该字段分配默认值. 默认值的字段允许为空. 对默认值字段也可以插入null. CREATE TABLE STUDENT( I ...
- 使用resultMap定义查询结果集,实现关联查询
接下来介绍resultMap定义查询结果集,实现关联查询 1 首先在接口中定义操作的方法 public interface EmployeeMapperPlus { public Employee g ...
随机推荐
- html-proload
对基础与初始化进行预加载 加载顺序 document > preload > 正常加载 media 属性 不同设备时的响应式加载 - media="(max-width: 600 ...
- 今日js心得
<input type="hidden" id="history_chart_json" data-json="#{smartDeviceMon ...
- Navicat for MySQL 设置定时任务(事件)
1.查询界面输入命令,查看定时任务是否开启,未开始时OFF: show variables like '%event_scheduler%'; 2. 查询界面输入命令,开启定时任务: set glob ...
- 使用ABAP批量下载Markdown源文件里的图片到本地
执行我github里的这个report: 选中一段markdown文档,ctrl C: 然后直接执行report: 执行完毕: 所有文件都下载到本地文件夹: 这个report使用到的工具类:zcl_c ...
- sed原理及sed命令格式 ,缓存区,模式空间
4.1 Sed工作原理 sed是一个非交互式的流编辑器.所谓非交互式,是指使用sed只能在命令行下输入编辑命令来编辑文本,然后在屏幕上查看输出:而所谓流编辑器,是指sed每次只从 ...
- Linux学习笔记(四)Linux常用命令:帮助命令
一.帮助命令man [man] [命令] 例如: man ls man的级别 man -f [命令] 相当于 whereis [命令] 可查看该命令有几个等级,进而可以通过 man [等级数] [ ...
- Linux查找并杀死僵尸进程(转)
1.查看系统是否有僵尸进程 使用Top命令查找,当zombie前的数量不为0时,即系统内存在相应数量的僵尸进程. 2.定位僵尸进程 使用命令ps -A -ostat,ppid,pid,cmd |gre ...
- mysql——数据库存储引擎
MyISAM INNODB myISAM.innodb.memory MyISAM对事务要求不高,以查询和添加为主,考虑使用. 如bbs中的发帖表,回复表: INNODB存储引擎 对事务要求高,保存的 ...
- LoadRunner(8)
一.脚本关联技术 引入: 打开WebTours首页,点击administration连接: 具有大量管理项,LR为了模拟一些特效设置的选项,实际项目中不存在. -> 选择第三项: Set LO ...
- [六省联考2017]分手是祝愿——期望DP
原题戳这里 首先可以确定的是最优策略一定是从大到小开始,遇到亮的就关掉,因此我们可以\(O(nlogn)\)的预处理出初始局面需要的最小操作次数\(tot\). 然后容(hen)易(nan)发现即使加 ...