sql >> Database teknologi >  >> RDS >> Mysql

MySQL-forespørgsel, grupper efter og derefter sorteret efter seneste grupperet med betingelse

Jeg har tilføjet dette i where-klausulen, og det ser ud til at virke

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

Fuldt opdateret kode

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



  1. Kan ikke indlæse DLL 'SqlServerSpatial.dll'

  2. Tjek hvor gammel en Oracle-database er?

  3. Skal jeg bruge SQL_Variant-datatypen?

  4. Opdatering af postgreSQL DB gennem EFCore-migreringer i Azure DevOps