`

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;

 

 

  • 大小: 90.1 KB
  • 大小: 97.8 KB
  • 大小: 113.5 KB
  • 大小: 246 KB
  • 大小: 537.1 KB
  • 大小: 369.7 KB
  • 大小: 128.3 KB
  • 大小: 174.9 KB
  • 大小: 152.4 KB
  • 大小: 110.2 KB
分享到:
评论

相关推荐

    mysql 树形结构查询

    这种查询方式可以高效地查询树形结构的数据,并且可以根据需要设置递归深度。 MySQL 中的树形结构查询可以使用存储过程来实现,存储过程是一种复杂的查询逻辑,可以将复杂的查询逻辑封装在存储过程中,以提高查询...

    MySql 中查询树形结构的全部子项列表 Function

    本文将深入探讨如何在MySQL中查询树形结构的全部子项列表,结合提供的`MySql_Link_Function.sql`文件,我们将探讨一种有效的方法来实现这一功能。 首先,树形结构在数据库中的存储通常采用自引用的方式,即每个节点...

    国家开放大学 MySQL数据库应用 实验训练2:数据查询操作

    查询列出所有用户 ID,以及他们的评论,如果有的话,需要使用外连接查询将用户表和评论表连接。 本实验训练旨在让学生掌握 MySQL 数据库应用中的数据查询操作,包括字段查询、多条件查询、DISTINCT、ORDER BY、...

    mysql数据表导出生成xml文件和树形结构

    然后,利用这个树结构,我们可以开发一个用户界面,展示数据表的树形视图。用户可以通过点击节点来加载或隐藏子节点,从而实现交互式浏览。 总之,MySQL数据表导出到XML文件是一个涉及数据库连接、数据查询、XML...

    Ajax+jsp+MySQL实现动态树形菜单

    再来说说**MySQL**,这是一个广泛使用的开源关系型数据库管理系统,用于存储和管理树形菜单的数据。在动态树形菜单中,每个菜单项通常对应数据库中的一条记录,包含ID、父ID、名称等字段。当JSP接收到Ajax请求后,会...

    国家开放大学 MySQL数据库应用 实验训练4:数据库系统维护

    视图是一种基于基表的虚拟表,它提供了一种简洁的方式来查询和操作数据。视图可以单源也可以多源,可以根据需要定义不同的视图来满足不同的查询需求。 在本实验中,我们定义了多种类型的视图,包括单源视图、多源...

    在java中 遍历mysql中的树形结构

    在MySQL数据库中,表示树形结构的表通常包含以下字段: - `id`:主键,用于唯一标识每个节点。 - `parent_id`:外键,指向其父节点的`id`,根节点的`parent_id`通常为NULL或特殊值。 - `subCode`:节点的代码或...

    MySQL查询把多列返回结果集拼装成一个字段

    mysql中有种可以通过join相关操作进行表与表之间的方式查询不同结果集,但是在一对多的情况下,关键查询的结果是多条的.例如:班级和学习的关系,我想很直观的看到班级和学生的情况,列表显示出班级的信息和班级的男生...

    mysql数据库应用形考任务(实训1~4.zip)

    分析:条件和查询对象存在于评论表中,对此商品发表过评论的用户不止一个,而且一个用户可以对此商品发表多个评论,因此,结果需要进行去重,这里使用DISTINCT实现。 (2)查询此汽车用品网上商城会员的创建时间段,...

    JSP+Mysql实现的简单树形结构

    在这个项目中,树形结构的节点数据可能存储在MySQL数据库的表中,每个节点可能包含父节点ID、子节点列表等信息,以实现树形关系。 3. **数据模型**:为了实现树形结构,通常需要设计一个合理的数据模型。在这个项目...

    mysql数据查询操作-实验训练2.docx

    本实验训练涵盖了 MySQL 数据查询操作的多个方面,包括单表查询、多条件查询、聚合函数查询、内连接查询、外连接查询、复合查询等。通过对实验内容的分析,可以总结出以下知识点: 1. 单表查询: * 了解如何使用 ...

    BS实现树形结构(jsp+mysql数据库+设计文档)

    本项目“BS实现树形结构(jsp+mysql数据库+设计文档)”提供了一个完整的解决方案,包括源码、开发文档以及SQL Server数据库,方便开发者在MyEclipse环境中直接导入使用。 首先,我们来探讨树形结构在Web开发中的应用...

    改进的多级树形菜单dtree,eclipse开发。mysql数据库,S2SH架构—(1)

    改进的多级树形菜单dtree,eclipse开发。mysql数据库,S2SH架构,有源码和jar包。 共两部分。这是第一部分

    Mysql查询流程分析

    如果之后有相同的查询再次执行,MySQL可以直接从缓存中获取结果而无需重新执行查询,从而大大提高了查询速度。然而,需要注意的是,查询缓存的使用可能会受到某些因素的影响,例如缓存空间大小限制以及缓存失效策略...

    国开MySQL数据库应用形考任务1~4.doc

    MySQL 数据库应用形考任务 1~4 的知识点摘要 实验 1:数据库创建和管理 * 创建数据库和表的方法 * MySQL 支持的数据类型 * 数据完整性在 MySQL 下的表现形式 * CREATE TABLE 语句的操作方法 * SHOW TABLES 语句的...

    BS树形结构(jsp+mysql数据库+设计文档)

    在这个项目中,“BS树形结构(jsp+mysql数据库+设计文档)”是利用Java Web技术,即JSP(JavaServer Pages)和Servlet,以及MySQL数据库来实现的Web应用。下面将详细阐述这个项目的知识点。 1. **B/S架构**:B/S...

    jsp+mysql+java 写的树形

    【标题】"jsp+mysql+java 写的树形" 指的是一种基于Web的交互式用户界面设计,其中数据以树形结构展示。在Web开发中,这种树形结构常用于展示层级关系的数据,如目录结构、组织架构或者数据库表的关联关系等。JSP...

    excel树形字典,导入数据库

    7. 树形结构重建:导入完成后,根据保存的父ID关系,使用递归查询或层次查询(如MySQL的WITH语句)来构建树形结构,便于后续的查询和操作。 8. 数据验证:导入后,对比Excel数据和数据库数据,确保所有信息都被正确...

Global site tag (gtag.js) - Google Analytics