0

I have achieved this sql statement:

SELECT users.firstname, users.lastname, users.screenname, profile.profile_pic,
users.user_id, messages.msgid, messages.message, messages.action,
DATE_FORMAT(messages.date_sent, '%M %d, %Y %r') AS date_sent
FROM messages messages
JOIN users ON messages.sent_by = users.user_id
LEFT JOIN profile ON (users.user_id = profile.user_id)
WHERE messages.recepient_id = "12052821" and messages.action = false
ORDER BY msgid DESC

And it outputs me the correct output (below) enter image description here

The problem is how do you sort out all the list of the users who messaged you and display the top 1 messages ORDER BY by msgid DESC respectively? (Users must not be repeated)

Here are my tables enter image description here

My query above needs a little bit of enhancement I guess. Thank you guys in advance

2 Answers 2

0

Problem Solved - just need to add this line: GROUP BY users.user_id before order by

-1

Try

ORDER BY msgid DESC LIMIT 1
2
  • Lool. ORDER BY msgid DESC LIMIT 1 = simply gives you 1 output in DESCENDING order out of 10 for example Jun 1, 2012 at 5:25
  • The question does necessarily wants to have distinct values. When two or more rows have the same user_id, the output should give you only 1 distinct user_id from the same rows not LIMIT. Jun 1, 2012 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.