mysql - Order by not working as expected after using group by -
i attempting list of messages, grouping from_user_id
, to_user_id
screenshot below. only, problem not seem ordering recent.
here's screenshot showing how look:
query: select
select concat(to_user_id, from_user_id) group_by, pm.* personal_messages pm (to_user_id = 1265) or (from_user_id = 1265) group group_by order id desc
table structure:
you need add date value query:
select concat(to_user_id, from_user_id) group_by, max(updated_at) max_updated_at personal_messages pm (to_user_id = 1265) or (from_user_id = 1265) group group_by order max_updated_at desc
the date value created_at
or updated_at
, depends on actual requirement. have use aggregate function max
though, because neither of these fields appears in group by
clause.
Comments
Post a Comment