mysql - 有限制地检索评论和回复

标签 mysql post join comments limit

我仍然没有找到正确的方法来按照我想要的方式去做。我试图实现的是评论帖子部分,它将显示该帖子的最后 2 条回复

我的表结构是这样的

table post
---------------------
post_id | comment | post_user | post_date

post_reply
---------------------
reply_id | parent_id | reply_user | reply_date

实际上我能够获取所有帖子和所有回复,但我面临的问题是我想限制我获取的帖子数量以及我获取的每个帖子的回复数量,我也想限制如果没有回复则显示帖子,结果应如下所示

result
----------------------------------------------
post 1
reply post 1
reply post 1
post 2 
post 3
reply post 3 
reply post 3

当然,我想显示这个,直到我达到 10 个帖子,并且每个帖子最多附加 2 个回复,当然查询可能会更少。实际上,我对每个帖子使用一段时间的查询,最后 2 个回复,但每次使用 11 个查询,也许你们知道更好的方法来减少查询

最佳答案

我假设您的 ID 列的类型为 INT。如果没有,您可能必须使用变量来计算每个帖子的回复数,如here所述。 .

所以试试这个

SELECT 'POST' AS type, p1.post_id, p1.comment, p1.post_user, p1.post_date, '-' AS reply_id
 FROM post p1
 LIMIT 10

UNION

SELECT 'REPLY' AS type, pr.parent_id AS post_id, 
 '-' AS comment, pr.reply_user AS post_user, pr.reply_date AS post_date, pr.reply_id
 FROM post_reply pr
 JOIN (
     SELECT p2.post_id 
     FROM post p2 
     LIMIT 10
 ) AS tmpPost ON pr.parent_id = tmpPost.post_id
 JOIN (
     SELECT pr.reply_id, COUNT(*) AS row_number
     FROM post_reply pr 
     JOIN post_reply pr2 ON pr.parent_id = pr2.parent_id AND pr.reply_id >= pr2.reply_id
     GROUP BY pr.reply_id
 ) AS tmpPostRN ON tmpPostRN.reply_id = pr.reply_id
 WHERE tmpPostRN.row_number <= 2

ORDER BY post_id ASC, type ASC, reply_id ASC

结果会是这样的

+-------+---------+-----------+-----------------+------------+----------+
| type  | post_id | comment   | post_user       | post_date  | reply_id |
+-------+---------+-----------+-----------------+------------+----------+
| POST  |       1 | comment1  | user1           | 0000-00-00 | -1       |
| REPLY |       1 | -         | post 1 reply1   | 0000-00-00 | 1        |
| REPLY |       1 | -         | post 1 reply 2  | 0000-00-00 | 2        |
| POST  |       2 | comment2  | user2           | 0000-00-00 | -1       |
| POST  |       3 | comment3  | user3           | 0000-00-00 | -1       |
| REPLY |       3 | -         | post 3 reply 1  | 0000-00-00 | 3        |
| REPLY |       3 | -         | post 3 reply 2  | 0000-00-00 | 4        |
| POST  |       4 | comment4  | user 4          | 0000-00-00 | -1       |
| POST  |       5 | comment5  | user 5          | 0000-00-00 | -1       |
| POST  |       6 | comment6  | user6           | 0000-00-00 | -1       |
| POST  |       7 | comment7  | user7           | 0000-00-00 | -1       |
| POST  |       8 | comment8  | user8           | 0000-00-00 | -1       |
| POST  |       9 | comment9  | user9           | 0000-00-00 | -1       |
| POST  |      10 | comment10 | user10          | 0000-00-00 | -1       |
| REPLY |      10 | -         | post 10 reply 1 | 0000-00-00 | 5        |
| REPLY |      10 | -         | post 10 reply 2 | 0000-00-00 | 6        |
+-------+---------+-----------+-----------------+------------+----------+

您可以在 post_reply 表中插入评论列(前提是您有一个),而不是第二个查询中的 '-' AS comment。您还可以将帖子的reply_id 设置为您选择的默认值(而不是-1)。

关于mysql - 有限制地检索评论和回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243504/

相关文章:

android - retrofit 2 动态设置零件参数名称

php sql更新加入

python - 如果它们在 Python 中不为空,则连接多个字符串

javascript - 使用从 PHP 生成的 HTML 调用的函数 AJAX 更新 MYSQL 数据库

javascript - Knex.js 和 MySQL : Casting Integer into Boolean for bulk select

javascript检查$_POST是否存在

php - 如何从 mySQL 中的索引字段输出其他表的值?

PHP 我的查询在 phpmyadmin 中有效,但在我的代码中无效

mysql - MySQL 数据库中的文件处理?

java - 在一行中获取整个 POST 响应