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 ...
随机推荐
- Ubuntu 上网
1.打开终端 2.sudo gedit /etc/wpa_supplicant/wpa_supplicant.conf(回车之后会弹出一个编辑页面,在里面打入第三步里面的配置文件)3.配置文件如下:c ...
- PM2 部署 nodejs API项目
PM2的主要特性: 内建负载均衡(使用Node cluster 集群模块) 后台运行 0秒停机重载,我理解大概意思是维护升级的时候不需要停机. 具有Ubuntu和CentOS 的启动脚本 停止不稳定的 ...
- Stanford NLP 课程笔记之计算字符串距离
在自然语言处理任务中,有时候需要计算两个字符串之间的相似度,也可以称作是两者之间的距离,用最小编辑距离表示. 最小编辑距离用{Insertion,Deletion,Substitution}这三种操作 ...
- nuxt项目打包上线之二
之前写过一篇nuxt打包上线的文章,请看这里:https://www.cnblogs.com/daisygogogo/p/11218809.html 上一篇文章的部署流程有点不好的地方,就是它适用于只 ...
- vsCode设置代码片段
输入vue.json { "Print to console": { "prefix": "vv", "body": [ ...
- VMware无法连接MKS:套接字连接尝试次数太多解决
粘贴自:https://jingyan.baidu.com/article/425e69e61eb578be15fc16ae.html VMware在开启虚拟机的时候,突然弹出[无法连接MKS:套接字 ...
- CSP2019 退役记
本来想写"退役在即"的,考完 Day2 后直接改成"退役记"了 Day 0 在 ssf 的机房里继续变弱,自己写了一遍 splay 板子,居然写对了,开心 非常 ...
- 红米k30评测+全面解读
暂时还没有图片等页面美化..其实网上有很多美图,这里不再粘贴了 红米k30是小米公司子品牌红米最近推出的一款性价比非常高的手机,以下进行多方面解读: 本人会尽量用不懂手机都能理解的语言来各方面讲解k3 ...
- [NOI2014]购票——斜率优化+树链剖分+线段树
建议到UOJ上去交 题解 一眼\(DP\),先把转移方程写出来 设\(dp[i]\)为从点\(i\)出发到点\(1\)的最小费用,那么存在转移 \[f[i]=min\{f[j]+(d[i]-d[j]) ...
- Centos6.5 下安装配置Apache+PHP+Mysql环境
1.准备工作 # yum -y update && yum -y install vim make cmake gcc gcc-c++ bison bison-devel ncurse ...