Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL Query, agrupar por e depois ordenar pelo mais recente agrupado com condição


Eu adicionei isso na cláusula where e parece estar funcionando
WHERE 
'$myid' IN ( senderID ) AND senderdelete = 0 
OR
'$myid' IN ( receiverID ) AND receiverdelete = 0 

Código atualizado completo
 $query_messages = mysqli_query($link,"SELECT 
      PreQuery.*,
      m2.message,
      m2.senderID,
      m2.receiverID,
      m2.senderdelete,
      m2.receiverdelete
   from
      ( SELECT 
              if( m.senderID < m.receiverID, m.senderID, m.receiverID ) as Person1,
              if( m.senderID < m.receiverID, m.receiverID, m.senderID ) as Person2,
              max( m.messageID ) as LastMessageIDPerChat,
              max( m.date ) as LastMessageDate
           FROM 
              messages m
           WHERE 
                   '$myid' IN ( senderID) AND senderdelete = 0 OR '$myid' IN ( receiverID) AND receiverdelete = 0 

           GROUP BY
              Person1, 
              Person2
           ORDER BY 
              m.date ASC
           LIMIT 10 ) PreQuery

         JOIN messages m2
            on PreQuery.LastMessageIDPerChat = m2.messageID    ORDER BY 
              date DESC");
$row_messages = mysqli_fetch_assoc($query_messages);
$totalRows_messages = mysqli_num_rows($query_messages);