0

MYSQL query is getting data exactly as I want.But ORDER BY clause is not working.

But when I removed the GROUP BY from query then ORDER BY clause is work.

SELECT MESS. * , US. * , LF. *
    FROM messages MESS
    LEFT JOIN users AS US ON US.id = MESS.to_id
    LEFT JOIN label_infos LF ON LF.user_id = MESS.to_id
    WHERE MESS.frm_id =27
    OR MESS.to_id =27
    GROUP BY US.id
    ORDER BY MESS.id DESC
Elshan
  • 7,339
  • 4
  • 71
  • 106
kaya new
  • 123
  • 1
  • 13
  • 2
    Just because MySQL allows this query does not mean you should use it. Read about aggregation and grouping. – juergen d Jun 16 '16 at 10:28
  • 2
    Possible duplicate of [MySQL "Group By" and "Order By"](http://stackoverflow.com/questions/1066453/mysql-group-by-and-order-by) – Shadow Jun 16 '16 at 10:44
  • 1
    Some example data & expected results would help here. But if there can be multiple MESS.id values for each US.id that you are grouping by - then it is indeterminate which value US.id you get back. The latest version of MySQL by default disallows non-aggregated columns being return unless you explicitly wrap them in ANY_VALUE(). – PaulF Jun 16 '16 at 11:30

0 Answers0