M
M
Maxim Uglov2014-06-02 22:31:57
MySQL
Maxim Uglov, 2014-06-02 22:31:57

Mysql query from two interdependent tables

Table Users and Comments
It is necessary to display a list of users who wrote the last comments.
select * from comments as com
left join users as us on com.user_id=us.user_id
group by user_id order by com.date desc LIMIT 0,10
Displays not the latest by comments, but random ones.
if without group by
select * from comments as com
left join users as us on com.user_id=us.user_id
order by com.date desc LIMIT 0,10
then it shows users repeats if they wrote the last 2 comments.
help me please

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mandor, 2014-06-03
@Mandor

Try "distinct user_id".

V
Vladimir Smirnov, 2014-06-03
@bobzer

If you need users exactly, then it will be more convenient to take the main query table users:

select distinct us.* from users us
inner join comments com on com.user_id = us.user_id
order by com.date

P
Pavel Solovyov, 2014-06-02
@pavel_salauyou

try group by com.user_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question