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 ...
随机推荐
- SSE指令集加速之 I420转BGR24
void yuv420_to_rgb24_sse3(uint8_t *yp, uint8_t *up, uint8_t *vp, int sy, int suv, int width, int hei ...
- gitlab 搭建
一.ubuntu搭建gitlab 1. 如果以前有安装过gitlab请根据以下步骤来删除 https://www.cnblogs.com/shansongxian/p/6678110.htm ...
- asp.net 简单的身份验证
1 通常我们希望已经通过身份验证的才能够登录到网站的后台管理界面,对于asp.net 介绍一种简单的身份验证方式 首先在webconfig文件中添加如下的代码 <!--身份验证--> &l ...
- c# datatable 如何转CSV文件
public void DataTableToCSV(DataTable dtCSV, string csvFileFullName, bool writeHeader, string delimet ...
- world 页面横向
今天浏览world时,发现一个现象: 出现的页面比较宽,如何做的呢? 操作如下: 1.打开一个新的world 2.选择布局页签,分隔符,分节符的下一页 3.鼠标放到第二页上,选择页签布局,纸张方向,选 ...
- Vmvare 虚拟机固定IP
首先我们打开虚拟机的虚拟网络编辑器,打开vmvare菜单栏的编辑,选择虚拟网络编辑器. 在打开的网络虚拟器中,会看到相关信息,虚拟机网络类型采用的NAT模式,子网地址是192.168.89.0,虚 ...
- identity server4获取token和userInfo
一.简介 IdentityServer4(ids4)是用于ASP.NET Core的OpenID Connect和OAuth 2.0框架.在许多成熟的.net core框架中都完美的集成的该身份服务框 ...
- CentOS7 解决不能切换中英文输入法的问题
1. 运行 im-chooser(如果没有要先安装) $ im-chooser 2. 在打开的窗口选择 iBus,然后 Logout 再 Login, 输入法即可切换.
- 使用pipenv管理你的python项目
怎么使用pipenv管理你的python项目 原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with- ...
- 如何用 tensorflow serving 部署服务
第一步,读一读这篇博客 https://www.jb51.net/article/138932.htm (浅谈Tensorflow模型的保存与恢复加载) 第二步: 参考博客: https://blog ...