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 ...
随机推荐
- 2.JVM、JRE、JDK之间的关系
诠释一. JDK(java development kit) JDK是Java开发工具包,是整个Java的核心,包括了Java运行环境JRE.Java工具和Java基础类库. JRE(java run ...
- NPOI 实现在已存在的Excel中任意位置开始插入任意数量行,并填充数据
1 npoi版本2.1.3.1 2 需要添加的引用: using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using System.IO;using N ...
- ECharts简单入门demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- RPC、RMI、REST的区别
初入职场,接触了不少企业常用的技术,与学校实训使用的技术有很大差异,在这里记录一下RPC.RMI与REST的区别. 概念 RPC(Remote Procedure Call,远程过程调用) 一种通过从 ...
- 使用nodejs创建Marketing Cloud的contact数据
源代码如下: var config = require("./mcConfig"); var request = require('request'); var url = con ...
- 2.SpringMVC执行流程
SpringMVC 执行流程: 执行流程简单分析: 1.浏览器提交请求到中央调度器 2.中央调度器直接将请求转给处理器映射器 3.处理器映射器会根据请求,找到处理该请求的处理器,并将其封装为处理器执行 ...
- static{}静态代码块与{}普通代码块之间的区别
先看一个例子: //class A package com.my.test; class A { static { System.out.println("A1:父类静态代码区域" ...
- svn中日志不展示解决方法记录
一,问题:点击svn查看日志时不显示.筛选时间显示为1970 1,猜想可能没有查看日志权限 2,查看linux 下svn版本库 confg 下三个配制文件 authz ,passwd ,svnserv ...
- kubernetes之健康状态检测
1.说明 容器探针: kubelet 对容器执行的定期诊断 探针执行方式: LivenessProbe: 判断容器是否存活 running状态, 如果不健康kubelet就会杀掉pod,根据重启策略R ...
- linux下devel软件包作用
devel 包主要是供开发用,至少包括以下2个东西: 头文件 链接库 有的还含有开发文档或演示代码. 以 glib 和 glib-devel 为例: 如果你安装基于 glib 开发的程序,只需要安装 ...