Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL-Abfrage, Gruppieren nach und dann Sortieren nach der neuesten Gruppierung mit Bedingung

Ich habe dies in der where-Klausel hinzugefügt und es scheint zu funktionieren

WHERE 
'$myid' IN ( senderID ) AND senderdelete = 0 
OR
'$myid' IN ( receiverID ) AND receiverdelete = 0 

Vollständig aktualisierter Code

 $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);