Mysql
 sql >> Base de données >  >> RDS >> Mysql

GROUP BY et ORDER BY

Utiliser :

  SELECT a.id, 
         a.msg
    FROM USERS_PM a
   WHERE a.dialog_id = 1
     AND 1 IN (a.uid, a.bid)
ORDER BY a.date DESC
   LIMIT 1

...ou :

SELECT a.id, 
       a.msg
  FROM USERS_PM a
  JOIN (SELECT t.dialog_id,
               MAX(t.date) AS max_date
          FROM USERS_PM t
      GROUP BY t.dialog_id) b ON b.dialog_id = a.dialog_id
                             AND b.max_date = a.date
 WHERE a.dialog_id = 1
   AND 1 IN (a.uid, a.bid)