-
problems with group by
Hi everybody, here I am the last day of the year with one problem...
this is my table:
Code:
id useridfrom useridto message
1 5 15 A
2 5 15 B
3 10 8 C
4 2 15 D
5 10 15 E
6 2 6 F
7 10 15 G
what I want is:
Code:
id useridfrom message
2 5 B
4 2 D
7 10 G
for one useridto (for example, useridto=15) I want the max(id) for each useridfrom
I thought I could use this query:
Code:
SELECT max( id ) , useridfrom, message
FROM messages
WHERE useridto =15
GROUP BY useridfrom
but the result doesnīt seem to be correct
any idea about how can I do it? if itīs not possible with group, how could I do it?
Iīm using mysql 4.0.22
thanks and happy new year :)
-
Code:
select t1.id
, t1.useridfrom
, t1.message
from messages as t1
inner
join messages as t2
on t1.useridto = t2.useridto
and t1.useridfrom = t2.useridfrom
where t1.useridto = 15
group
by t1.id
, t1.useridfrom
, t1.message
having t1.id
= max(t2.id)
-
Thanks very much,
thatīs exactly what I need
:D thanks again