mysql - How to order by first record of another order by result? -
i trying create own messaging sms in phones.
i have mysql table this
id (the message id) from_member_id (the id of member sent message) to_member_id (the id of member message sent to) date sent (the date sent) active (if message deleted or active) text (the text)
and want information in special ordered way.
first has sorted id that's not (call 'other' id). each section of ordering, needs top record (which should recent date), , sort sections date value of record.
i can first ordering this:
select from_member_id, to_member_id, (case when from_member_id = ? to_member_id else from_member_id end case) conversation_member_id, date_sent table from_member_id = ? or to_member_id = ? order conversation_member_id desc, date_sent desc
where ?
id.
but problem how second ordering, need order sections date of top record (which should recent date). note when section, mean group of records same 'other' id.
does know how this?
thanks.
edit:
example
if id 5:
from date 5 6 july 28 6 5 july 7 6 5 july 7 5 2 july 26 5 2 july 26 2 5 july 26
so july 28 date should come on top, because happened though there dates in section before july 26.
so when first ordering, orders other id, 6, , 2. ones 6 1 section, , ones 2 second section.
i'm ms sql guy, trust clear enough:
create table #messages ( fromid int not null, toid int not null, date datetime2(0) not null, text varchar(200) not null ) insert #messages (fromid, toid, date, text) values (5, 6, '2013-07-28 12:00', 'a'), (6, 5, '2013-07-07 02:00', 'b'), (6, 5, '2013-07-07 01:00', 'c'), (5, 2, '2013-07-26 03:00', 'd'), (5, 2, '2013-07-26 02:00', 'e'), (2, 5, '2013-07-26 01:00', 'f') declare @me int = 5 select m.fromid, m.toid, m.date, m.text #messages m inner join ( select case when fromid = @me toid else fromid end id, max(date) mostrecentdate #messages group case when fromid = @me toid else fromid end ) seq on case when m.fromid = @me m.toid else m.fromid end = seq.id order seq.mostrecentdate desc, m.date desc
Comments
Post a Comment