gpt4 book ai didi

Mysql查询GROUP BY where条件LIMIT 1

转载 作者:可可西里 更新时间:2023-11-01 08:36:12 27 4
gpt4 key购买 nike

我在数据库中有一个名为“articles”的表

文章:

id +++   writer_id    +++     title     ++++    text
----------------------------------------------------
1 1 some title article for writer 1
2 1 some title article for writer 1
3 2 some title article for writer 2
4 2 some title article for writer 2
5 2 some title article for writer 2
6 3 some title article for writer 3

我需要一个查询来从表中获取最新的文章,但每个作者只有一篇文章

取决于上面的行查询应该只得到 3 行:每篇文章归一位作者所有

2           1              some title        article for writer 1 
5 2 some title article for writer 2
6 3 some title article for writer 3

PHP

SELECT * 
FROM articles
WHERE writer_id = ???? ;
order by id desc limit 10

提前致谢:)

最佳答案

简短而甜美:

SELECT MAX(id), writer_id, title, text FROM articles GROUP BY writer_id;

关于Mysql查询GROUP BY where条件LIMIT 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11803700/

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