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;
好的,到此content返回的结果为我们最终想要的结果了!
6、补充:如果要根据说说id求出对应的评论及回复列表:
求说说id=1000的评论及回复sql语句如下:
select if(T.type='t2',T.createDate2,T.createDate3) as createDate, T.type, T.dataId, T.customerId, T.content from((select nc.createDate as createDate2, "" as createDate3, "t2" as type, nc.customerId, nc.dataId, concat('发表评论:',nc.content) as content from ixt_customer_note_comment nc where nc.dataId='1000' order by nc.createDate asc) union (select 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 where nc.dataId='1000' order by nc.createDate asc, nr.createDate asc)) as T order by createDate2 asc, createDate3 asc;
myql 查询树形表结果:说说、说说的评论、评论的回复相关推荐
- 树形表的平行查询设计
本文由网友长空X投稿,欢迎转载.分享 原文作者:长空X(CSDN同名"长空X",CkTools的作者,github: https://github.com/hjkl950217) ...
- mybatis mysql查询树形结构_MyBatis collection 集合嵌套查询树形节点
原标题:MyBatis collection 集合嵌套查询树形节点 MyBatis collection 集合 MyBatis 是数据持久层框架,支持定制化 SQL.存储过程以及高级映射.尤其强大在于 ...
- mysql所有班级名称和人数_mysql数据库优化课程---12、mysql嵌套和链接查询(查询user表中存在的所有班级的信息?)...
mysql数据库优化课程---12.mysql嵌套和链接查询(查询user表中存在的所有班级的信息?) 一.总结 一句话总结: in:distinct:select * from class wher ...
- mysql 所有表的字段信息_mysql如何查询所有表和字段信息
mysql查询所有表和字段信息的方法: 1.根据库名获取所有表的信息 SELECT * FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = 'e ...
- java查询mysql装载bean_jsp与javabean链接mysql数据库并查询数据表的简单实例源码
jsp与javabean链接mysql数据库并查询数据表的简单实例源码.这个简单的实例是给新手学习的,或者一些高手临时忘记怎么使用jsp操作mysql数据库时候查找的,包括了建立mysql数据库连接的 ...
- ylb:SQL 表的高级查询-多表连接和子查询
ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...
- Sql查询一个表中除了某个字段以外的所有字段的方法
declare @name varchar(100) declare @sql varchar(1000)SET @sql='select ' DECLARE C11 CURSOR FORselect ...
- mysql查询员工表中所有员工入职20个月之后的日期_Mysql基础教程
往期推荐 2020黑马Python教程 Docker基础知识 网易云课堂2019 Java高级教程 郭术生AE教程 SQL语句分类 DDL:数据定义语言,用来定义数据库对象:库.表.列等 DML:数据 ...
- Navicat中查询哪些表有指定的字段名(技巧)
若要查询某个字段来自那张表,在navicat只能写sql来查询. 1.根据字段名查询表,查询哪些表有这个字段,SQL如下: 例如:哪些表中有permission_id这个字段: SELECT * FR ...
最新文章
- LeetCode简单题之数组序号转换
- Bitbucket Pipes发布,带来30+自动化CI/CD管道的方法
- Intel图形库Mesa的持续集成
- SQL2008中Merge的用法
- APIGEE – API网关简介
- 怎样写一篇优秀论文?看完受益匪浅!
- VSFTP安装配置总结
- 机器人技术创新与实践旧版本大纲
- 【备忘】Java菜鸟到大牛学习路线之实战篇
- mysql进阶教程pdf_SQL进阶教程 (MICK著) 高清pdf完整版
- 家里内网穿透远程控制学校电脑
- 计算机驱动空间的c盘不足怎么办,C盘磁盘空间不足怎么解决
- java文件输入流FileInputStream
- mysql 字符串转日期
- C#——检测鼠标滑轮事件
- VPP二层接口,不是翻墙
- 基于javaweb的在线车队货车管理系统(java+ssm+jsp+bootstrap+mysql)
- 计算机类大学生竞赛经验分享
- 打开excel服务器客户端无响应怎么办,excel服务器客户端登录不起
- 地下城与勇士游戏设计元素及成功原因分析