0

I have this query:

SELECT id, rdate, member_id
FROM msgs
WHERE wrote_to='1000'
AND tb1.member_id != '1002'
AND deleted != '1000'
GROUP BY member_id
ORDER BY rdate DESC

The problem is I get the messages but not the new one by rdate...

How can I order them by rdate and group by member_id ?... so I will get the newest message of each user?

Thanks.

1
  • 1
    Your alias of tb1 isn't valid Jul 20, 2011 at 8:33

3 Answers 3

0

Try:

SELECT * FROM (
      SELECT id, rdate, member_id FROM msgs 
       WHERE wrote_to = '1000' 
         AND tb1.member_id != '1002' 
         AND deleted != '1000' 
    ORDER BY rdate DESC
) tmp_msgs
GROUP BY member_id

This will have a little less performance, because of the temporary table, but that's a trade off you have to make for a working solution.

As @Brendan Bullen has noticed, why are you using the alias tb1. there?

0

Assuming a sequential ID:

SELECT id, rdate, member_id
FROM msgs
WHERE wrote_to='1000'
AND tb1.member_id != '1002'
AND deleted != '1000'
GROUP BY member_id
HAVING id = MAX(id)
ORDER BY rdate DESC

Using GROUP BY on a nested select is not reliable (even on ordered results) as the behaviour for columns not specified in the grouping is undefined.

0

rdate may be coming from another table or else you entered wrong coluomn name.

0

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.