mysql评论表如何查询,如何在MYSQL中发表评论回复查询?

I am having comment reply (only till one level) functionality. All comments can have as many as replies but no replies can have their further replies.

So my database table structure is like below

Id ParentId Comment

1 0 this is come sample comment text

2 0 this is come sample comment text

3 0 this is come sample comment text

4 1 this is come sample comment text

5 0 this is come sample comment text

6 3 this is come sample comment text

7 1 this is come sample comment text

In the above structures, commentid, 1 (has 2 replies) and 3 (1 reply) has replies. So to fetch the comments and their replies, one simple method is first I fetch all the comments having ParentId as 0 and then by running a while loop fetch all the replies of that particular commentId. But that seems to be running hundreds of queries if I'll have around 200 comments on a particular record.

So I want to make a query which will fetch Comments with their replies sequentially as following;

Id ParentId Comment

1 0 this is come sample comment text

4 1 this is come sample comment text

7 1 this is come sample comment text

2 0 this is come sample comment text

3 0 this is come sample comment text

6 3 this is come sample comment text

5 0 this is come sample comment text

I also have a comment date column in my comment table, if anyone wants to use this with comment query.

So finally I want to fetch all the comments and their replies by using one single mysql query. Please tell me how I can do that?

Thanks

解决方案

You can use an expression in an ORDER BY. Try this:

SELECT *

FROM comments

ORDER BY IF(ParentId = 0, Id, ParentId), Id

This will first sort by Id, if ParentId = 0, or by ParentId otherwise. The second sort criterion is the Id, to assure that replies are returned in order.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在设计 MySQL 结构时,应该考虑以下几点: 1. 数据类型:选择合适的数据类型来存储每一列的数据。 2. 主键:为设置一个主键,用于唯一标识的每一行。 3. 索引:为常用的查询列建立索引,提高查询效率。 4. 外键:在之间建立外键关系,保证数据的完整性和一致性。 5. 分区:考虑使用分区来提高大查询性能。 在设计时应该根据应用场景和预估的数据量来进行合理的设计,以保证在应用运行过程能够满足性能和可扩展性的需求。 ### 回答2: 动态评论MySQL结构设计主要包括评论和用户评论包括以下字段: - 评论ID:唯一标识符,主键 - 文章ID:被评论的文章的唯一标识符,外键关联文章 - 用户ID:发表评论的用户的唯一标识符,外键关联用户 - 评论内容:评论的具体内容,使用TEXT类型存储 - 父评论ID:示该评论是否是回复其他评论的,如果是,关联父评论评论ID - 创建时间:评论的创建时间,记录评论的时间戳 用户包括以下字段: - 用户ID:唯一标识符,主键 - 用户名:用户的用户名,唯一且不为空 - 密码:用户的密码,使用哈希算法加密存储 - 昵称:用户的昵称,可以为空 - 头像:用户上传的头像图片,使用BLOB类型存储 动态评论结构设计,为了方便评论查询和管理,可以在评论额外添加以下字段: - 点赞数:记录评论获得的点赞数量,方便按照点赞数排序 - 回复数:记录评论获得的回复数量,方便按照回复数排序 此外,为了提高查询效率,可以在评论添加索引,如按照文章ID、父评论ID、创建时间等字段创建索引,以优化查询性能。 总之,动态评论MySQL结构设计要根据实际需求来确定,上述设计仅为示例,可以根据具体情况进行调整和补充。 ### 回答3: 动态评论是指用户可以对某一条内容进行评论的功能。在设计MySQL结构时,可以考虑以下几个方面: 1. 用户:创建一个用户,用于存储用户的信息,如用户ID、用户名、头像等。用户可以和评论进行关联,以便查找评论是哪个用户发表的。 2. 内容:创建一个内容,用于存储用户发布的内容,如博客、新闻等。内容可以和评论进行关联,以便读取该内容下的所有评论。 3. 评论:创建一个评论,用于存储用户对内容的评论信息。评论可以包含以下字段:评论ID、用户ID、内容ID、评论内容、评论时间等。通过用户ID和内容ID与用户和内容进行关联,以获取评论对应的用户和内容信息。 4. 回复:如果需要支持用户对评论回复功能,可以创建一个回复,用于存储用户对评论回复信息。回复可以包含以下字段:回复ID、评论ID、用户ID、回复内容、回复时间等。通过评论ID与评论进行关联,以获取该回复所属的评论信息。 5. 索引设计:为了提高查询效率,可以在用户ID、内容ID、评论时间等字段上创建索引,以便快速查询对应的评论回复。 总体而言,动态评论MySQL结构设计需要考虑用户信息、内容信息和评论信息之间的关联,以及可能的回复功能。通过合理的结构设计和索引设计,可以满足对评论的查找、添加、删除等操作的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值