Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql query GROUP BY where condition LIMIT 1

Tags:

mysql

I have a table called "articles" on the database

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

I need a query to get the latest articles from the table BUT just ONE article for each Writer

depends on the rows above the query should get just 3 rows : each article owns by ONE writer

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

thanks in advance : )

like image 352
Ahmad Avatar asked Jan 28 '26 19:01

Ahmad


1 Answers

Short and sweet:

SELECT MAX(id), writer_id, title, text FROM articles GROUP BY writer_id;
like image 105
Kalpesh Avatar answered Jan 30 '26 10:01

Kalpesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!