gpt4 book ai didi

mysql - 在 MySQL 中获取不同的 ID

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:31 26 4
gpt4 key购买 nike

我有一个这样的表结构:

Comments table
id review_id user_id created comments deleted
1 10 28 2011-10-12 "hi" 0
2 11 28 2011-10-13 "yo" 0
3 10 28 2011-10-15 "bye" 0

我想从按创建的 DESC 排序的每条评论中获取最新评论。所以我有这样的东西:

SELECT DISTINCT review_id, 'comments' as type FROM comments as MyTable WHERE 
user_id=28 AND deleted=0 ORDER BY created DESC LIMIT 30

因此在上表中,我希望返回 id = 3 的行,然后返回 id = 2。

最佳答案

示例查询:

https://data.stackexchange.com/stackoverflow/s/2143/getting-distinct-ids-in-mysql

SELECT id,comment
FROM comments
WHERE id IN (
SELECT MAX(id)
FROM comments
WHERE user_id=28 AND deleted=0
GROUP BY review_id )
ORDER BY created DESC

关于mysql - 在 MySQL 中获取不同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com