作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
SELECT *, IFNULL(parent, id) AS p, IFNULL(reply_comment_id, id) AS r
FROM article_comments ORDER BY p ASC, r ASC, date DESC
我想使用 LIMIT。如果我有更多行,我想用“p”限制查询。在图像中: ("p": 1, 1, 1, 1, 1) – 这是一个,("p": 2, 2, 2, 2, 2, 2, 2) – 这是两个...
例如,我想:如果我使用 LIMIT 1,则只显示 ("p": 1, 1, 1, 1, 1)。
最佳答案
这是我要使用的查询:
SELECT * FROM
FROM article_comments
WHERE id = 1 OR parent = 1
ORDER BY parent ASC, reply_comment_id ASC, `date` DESC
请注意,我认为此处不需要您的计算列。当按父级和 reply_comment_id 升序排序时,NULL 值将排在第一位。
确保您在 parent、reply_comment_id 和日期字段上有索引(除了 id,我认为它是主键)
此外,您可能需要考虑为日期列使用日期时间字段,而不是 unix 时间戳。在查看数据库中的数据以及尝试查询日期范围时,它对用户非常友好/因此可以像这样编写过滤器:
WHERE `date` BETWEEN '2012-01-01 00:00:00' AND '2012-12-31 23:59:59'
不必进行时间戳转换。
关于mysql - 限制专精,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12610388/
我是一名优秀的程序员,十分优秀!