myql 查询树形表结果:说说、说说的评论、评论的回复

有三张表关联表:

用户的说说表(ixt_customer_note)

说说的评论表(ixt_customer_note_comment)

评论的回复表(ixt_customer_note_reply)

说说表保存的是用户发表的说说信息,评论表保存的是用户对说说发表的评论信息,回复表保存的是用户对评论及回复的回复

要求查询三张表返回结果为树形结构,如下:

发表说说:1003

发表说说:1002

发表评论:comment1002_1

发表评论:comment1002_2

发表回复:reply_1002_1

发表回复:reply_1002_2

发表评论:comment1002_3

发表说说:1001

发表评论:comment1001_1

发表评论:comment1001_2

发表说说:1000

发表评论:comment1000_1

发表回复:reply_1000_1

发表回复:reply_1000_2

1、设计三张表及插入相关数据

ixt_customer_note 表结构:

ixt_customer_note 表sql语句:

DROP TABLE IF EXISTS `ixt_customer_note`;

CREATE TABLE `ixt_customer_note` (

`id` varchar(50) NOT NULL COMMENT '主键UUID',

`customerId` varchar(50) NOT NULL COMMENT '用户id',

`content` varchar(500) NOT NULL COMMENT '说说内容',

`createUser` varchar(50) DEFAULT NULL COMMENT '创建人ID',

`createDate` datetime DEFAULT NULL COMMENT '创建时间',

`updateUser` varchar(50) DEFAULT NULL COMMENT '更新人ID',

`updateDate` datetime DEFAULT NULL COMMENT '更新时间',

`deleteFlag` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除标识:0未删除,1删除',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ixt_customer_note` VALUES ('1000', 'user1', '1000', null, '2015-10-01 21:18:24', null, null, '');

INSERT INTO `ixt_customer_note` VALUES ('1001', 'user1', '1001', null, '2015-10-06 21:18:19', null, null, '');

INSERT INTO `ixt_customer_note` VALUES ('1002', 'user1', '1002', null, '2015-10-14 22:05:04', null, null, '');

INSERT INTO `ixt_customer_note` VALUES ('1003', 'user1', '1003', null, '2015-10-15 21:18:12', null, null, '');

ixt_customer_note_comment 表结构:

ixt_customer_note_comment 表sql语句:

DROP TABLE IF EXISTS `ixt_customer_note_comment`;

CREATE TABLE `ixt_customer_note_comment` (

`id` varchar(50) NOT NULL COMMENT '主键UUID',

`customerId` varchar(50) NOT NULL COMMENT '评论用户ID',

`dataId` varchar(50) NOT NULL COMMENT '被评论的说说ID',

`content` varchar(1000) NOT NULL COMMENT '评论内容',

`createUser` varchar(50) DEFAULT NULL COMMENT '创建人ID',

`createDate` datetime DEFAULT NULL COMMENT '更新人ID',

`updateUser` varchar(50) DEFAULT NULL COMMENT '更新时间',

`updateDate` datetime DEFAULT NULL COMMENT '更新时间',

`deleteFlag` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除标识:0未删除,1删除',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ixt_customer_note_comment` VALUES ('1111', 'a1', '1001',
'comment1001_1', null, '2015-10-12 21:21:22', null, null, '');

INSERT INTO `ixt_customer_note_comment` VALUES ('1212', 'a2', '1001',
'comment1001_2', null, '2015-10-12 22:21:11', null, null, '');

INSERT INTO `ixt_customer_note_comment` VALUES ('2121', 'b3', '1002',
'comment1002_3', null, '2015-10-15 21:22:48', null, null, '');

INSERT INTO `ixt_customer_note_comment` VALUES ('321', 'b1', '1002',
'comment1002_1', null, '2015-10-14 21:21:59', null, null, '');

INSERT INTO `ixt_customer_note_comment` VALUES ('3221', 'c1', '1000',
'comment1000_1', null, '2015-10-02 21:23:19', null, null, '');

INSERT INTO `ixt_customer_note_comment` VALUES ('421', 'b2', '1002',
'comment1002_2', null, '2015-10-15 21:22:25', null, null, '');

ixt_customer_note_reply 表结构:

ixt_customer_note_reply 表sql语句:

DROP TABLE IF EXISTS `ixt_customer_note_reply`;

CREATE TABLE `ixt_customer_note_reply` (

`id` varchar(50) NOT NULL COMMENT '主键UUID',

`customerId` varchar(50) NOT NULL COMMENT '回复用户id',

`commentDataId` varchar(50) DEFAULT NULL COMMENT '被回复的评论ID',

`parentReplyDataId` varchar(50) DEFAULT NULL COMMENT '被回复的回复的id',

`content` varchar(1000) NOT NULL COMMENT '回复内容',

`createUser` varchar(50) DEFAULT NULL COMMENT '创建人ID',

`createDate` datetime DEFAULT NULL COMMENT '更新人ID',

`updateUser` varchar(50) DEFAULT NULL COMMENT '更新时间',

`updateDate` datetime DEFAULT NULL COMMENT '更新时间',

`deleteFlag` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除标识:0未删除,1删除',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ixt_customer_note_reply` VALUES ('1212', 'v1', '3221',
null, 'reply_1000_1', null, '2015-10-12 21:28:44', null, null, '');

INSERT INTO `ixt_customer_note_reply` VALUES ('3121', 'v2', '3221',
'1212', 'reply_1000_2', null, '2015-10-13 21:28:49', null, null, '');

INSERT INTO `ixt_customer_note_reply` VALUES ('431', 'v3', '421',
null, 'reply_1002_1', null, '2015-10-14 21:28:54', null, null, '');

INSERT INTO `ixt_customer_note_reply` VALUES ('5231', 'v4', '421',
'431', 'reply_1002_2', null, '2015-10-15 21:28:57', null, null, '');

2、分别查出三张表的数据:

2.1、查询用户说说表倒序显示

select createDate, id as dataId, customerId, concat('发表说说:',content) as content from ixt_customer_note order by createDate desc;

2.2、查询说说的评论正序显示

select nc.createDate, nc.dataId, nc.customerId,
concat('发表评论:',nc.content) as content from ixt_customer_note_comment nc
left join ixt_customer_note n on nc.dataId=n.id order by n.createDate
desc, nc.createDate asc;

2.3、查询说说的评论的回复正序显示

select nr.createDate, nc.dataId, nr.customerId,
concat('发表回复:',nr.content) as content from ixt_customer_note_reply nr
left join ixt_customer_note_comment nc on nr.commentDataId=nc.id left
join ixt_customer_note n on nc.dataId=n.id order by n.createDate desc,
nc.createDate asc, nr.createDate asc;

3、有了这三张表数据后,如何将他们显示为一张表,最终得到树形结构?

如果想要得到树形展示,可以考虑能否将三张表返回的结果合并为一张表,因为他们的结果合并在一起后正是我们需要的所有数据,只不过最终展示的效果要调整一下。

好的,先考虑合并用户说说及说说的评论,并按树形结构展示,这时我们应该使用 union关键字,求并集。观察一下,合并之后的结果集,应该先根据说说的发表时间倒序,再根据说说的评论的发表时间正序,所以写sql执行一下:

大致的语句为:select * from(说说的结果集 union 评论的结果集) as T order by 说说.createDate desc, 评论.createDate asc;

select * from((select createDate as createDate1, "" as createDate2,
id as dataId, customerId, concat('发表说说:',content) as content from
ixt_customer_note order by createDate desc) union (select n.createDate
as createDate1, nc.createDate as createDate2, nc.dataId, nc.customerId,
concat('    发表评论:',nc.content) as content from ixt_customer_note_comment
nc left join ixt_customer_note n on nc.dataId=n.id  order by
n.createDate desc, nc.createDate asc)) as T order by createDate1 desc,
createDate2 asc;

4、上面合并结果集是我们想要的结果,好的,再来合并回复结果集。合并之后的结果集应该按说说的发表时间倒序,再按评论的发表时间正序,再按回复的发表时间正序。为了区分出每条记录是哪张表的,我们在结果集中添加一个字段type,表示记录的类型:t1是说说,t2是评论,t3是回复。

sql语句:select * from(说说的结果集 union 评论的结果集 union 回复的结果集) as T order
by 说说.createDate desc, 评论.createDate asc, 回复.createDate asc;

select * from((select createDate as createDate1, "" as createDate2,
"" as createDate3, "t1" as type, id as dataId, customerId,
concat('发表说说:',content) as content from ixt_customer_note order by
createDate desc) union (select n.createDate as createDate1,
nc.createDate as createDate2, "" as createDate3, "t2" as type,
nc.dataId, nc.customerId, concat('    发表评论:',nc.content) as content from
ixt_customer_note_comment nc left join ixt_customer_note n
on nc.dataId=n.id  order by n.createDate desc, nc.createDate asc) union
(select n.createDate as createDate1, nc.createDate as createDate2,
nr.createDate as createDate3, "t3" as type, nc.dataId, nr.customerId,
concat('        发表回复:',nr.content) as content from
ixt_customer_note_reply nr left join ixt_customer_note_comment nc on
nr.commentDataId=nc.id left join ixt_customer_note n on nc.dataId=n.id
order by n.createDate desc, nc.createDate asc, nr.createDate asc)) as T
order by createDate1 desc, createDate2 asc, createDate3 asc;

5、上面结果集是我们想要的,不过createDate最终应该只有一个,可以继续改进,将createDate合并为一列,说说显示createDate1,评论显示createDate2,回复显示createDate3。

改进后的语句如下:

select
if(T.type='t1',T.createDate1,(if(T.type='t2',T.createDate2,T.createDate3)))
as createDate, T.type, T.dataId, T.customerId, T.content from((select
createDate as createDate1, "" as createDate2, "" as createDate3, "t1"
as type,customerId, id as dataId, concat('发表说说:',content) as content
from ixt_customer_note order by createDate desc) union (select
n.createDate as createDate1, nc.createDate as createDate2, "" as
createDate3, "t2" as type, nc.customerId, nc.dataId, concat('  
 发表评论:',nc.content) as content from ixt_customer_note_comment nc left
join ixt_customer_note n on nc.dataId=n.id  order by n.createDate desc,
nc.createDate asc) union (select n.createDate as createDate1,
nc.createDate as createDate2, nr.createDate as createDate3, "t3" as
type, nr.customerId, nc.dataId, concat('        发表回复:',nr.content) as
content from ixt_customer_note_reply nr left join
ixt_customer_note_comment nc on nr.commentDataId=nc.id left join
ixt_customer_note n on nc.dataId=n.id order by n.createDate desc,
nc.createDate asc, nr.createDate asc)) as T order by createDate1 desc,
createDate2 asc, createDate3 asc;

myql 查询树形表结果:说说、说说的述评、评论的回复的更多相关文章

  1. myql查询创建表语句SHOW CREATE TABLE table_name

    技术背景:刚开始学习MySQL时候,有时偷懒,会用SHOW CREATE TABLE 表名\G来复制表创建语句,可是当运行的时候总会因为"表名和列名上有单引号",提示语法错误不能运 ...

  2. oracle 树形表结构查询 排序

    oracle 树形表结构排序 select * from Table start with parentid is null connect by prior id=parentid order SI ...

  3. SQL Server 树形表非循环递归查询

    很多人可能想要查询整个树形表关联的内容都会通过循环递归来查...事实上在微软在SQL2005或以上版本就能用别的语法进行查询,下面是示例.   --通过子节点查询父节点WITH  TREE AS(  ...

  4. sql查询指定表外键约束

    //////////////////查询指定表外键约束select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外 ...

  5. SQL查询每个表的字段数量

    --SQL查询每个表的字段数量select b.[name], count(*) As AllCount,ISNULL(ISNULL(sum(case when isnullable=0 then 1 ...

  6. SQL server 查询某个表在哪些存储过程(SP)中使用到

    1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select ob ...

  7. SQL 查询所有表名、字段名、类型、长度、存储过程、视图

    -- 获得存储过程创建语句 select o.xtype,o.name,cm.text from syscomments cm inner join sysobjects o on o.id=cm.i ...

  8. 【转】oracle查询用户表,函数,储存过程,

    ◆Oracle查询用户表空间:select * from user_all_tables ◆Oracle查询所有函数和储存过程:select * from user_source ◆Oracle查询所 ...

  9. SQL语句查询某表的所有字段及数据类型

    SQL语句查询某表的所有字段及数据类型 SELECT name AS column_name , TYPE_NAME(system_type_id) AS column_type , max_leng ...

随机推荐

  1. 【数据库】_由2000W多条开房数据引发的思考、实践----给在校生的一个真实【练耙场】,同学们,来开始一次伟大的尝试吧。

      ×   缘起---闲逛博客园 前几天的时候,在某一QQ群看到一条消息“XXX酒店开房XXXBTXX迅雷BT下载”,当时是一目十行的心态浏览,目光掠过时, 第一反应我想多了~以为是XX种子(你懂的~ ...

  2. Python matplotlib笔记

    可视化的工具有很多,如Tableau,各种JS框架,我个人感觉应该是学JS最好,因为JS不需要环境,每个电脑都有浏览器,而像matplotlib需要Python这样的开发环境,还是比较麻烦的,但是毕竟 ...

  3. Python环境配置安装

    2016年12月20日14:15:23 -------------- 参考菜鸟教程: Python 环境搭建 | 菜鸟教程  http://www.runoob.com/python/python-i ...

  4. JKS和PKCS#12

    今天来点实际工作中的硬通货! 与计费系统打交道,少不了用到加密/解密实现.为了安全起见,通过非对称加密交换对称加密密钥更是不可或缺.那么需要通过什么载体传递非对称算法公钥/私钥信息?数字证书是公钥的载 ...

  5. Html中的文本框和单选按钮

    Html中的文本框和单选按钮用来制作页面的登录注册使用.. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN& ...

  6. MySql 小记

    MySql  简单 小记 以备查看 1.sql概述 1.什么是sql? 2.sql发展过程? 3.sql标准与方言的关系? 4.常用数据库? 5.MySql数据库安装? 2.关键概念 表结构----- ...

  7. 谷歌浏览器,火狐浏览器,ie浏览器解析顺序

    谷歌(版本 55.0.2883.87 m),火狐(48.0.2),ie(11.576.14393.0)对三个浏览器简单的进行了一下试验发现,谷歌浏览器是等到html全部解析完毕之后才开始渲染,而另外两 ...

  8. Django基础,Day6 - 单元测试tests

    在django项目app目录下,有个tests.py,我们通常可以直接在这文件中写我们的单元测试代码. test for a model 根据前面章节的操作步骤下来,在Question Model中有 ...

  9. Windows服务已经标记为删除

    一般卸载后..刷新一下服务列表就会消失不见..但是也会偶尔碰上一些钉子户.. 这时候其实重启一下机器就可以解决这个问题..会被回收掉..但是在服务器上..可不是随便都能重启的.. 这就到祭出杀手锏的时 ...

  10. springmvc @responsebody 406/415问题解决

    提供几个解决思路 1.如果项目中用的spring jar包是4.x版本, 需要jackson-annotations-2.x/jackson-core-2.x/jackson-databind-2.x ...