group by - Get right sorting over a grouped table in mysql -


i stuck following problem: have 2 tables:

 person   - uid   - first_name   - last_name mail   - uid (autoincrement)   - to_person -> 1:1 relation person.uid   - from_person -> 1:1 relation person.uid 

how can mails of person uid=1, sorted mail.uid desc , grouped person.uid? tried following:

select a.first_name,a.last_name,b.uid person a, mail b   (b.to_user=a.uid or b.from_user=a.uid) ,   (b.to_user=1 or b.from_user=1) ,   a.uid!=1 group a.uid order b.uid desc 

it give me exact amount of persons ordering latest mail not work.

anyone can give me idea how solve this?

thanks, dominic

edit:

here solution works if has same question:

select a.uid,a.first_name, a.last_name, b.max, c.tstamp, c.to_user, c.from_user person inner join   (select max(uid) max, to_user,from_user, if(to_user=13,from_user,to_user) other_user, tstamp   mail   (to_user=13 or from_user=13) , deleted=0 , hidden=0   group greatest(to_user,from_user), least(to_user,from_user)   order max desc) b on b.other_user=a.uid inner join   tx_intranet_domain_model_mailentry c on c.uid=b.max  a.deleted=0 , a.disable=0 

this because of in subquery not selecting timestamp that's why not recognize , order not working:

(select x.uid,x.from_user,x.to_user,x.timestamp mail x order x.uid desc)                                     ^^^ add here timestamp 

or can

(select x.* mail x order x.uid desc) 

as selecting fields.

well can use join.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -