Querying multiple MySQL data

0

I'm doing a chat and I have the following table in the database:

Iwanttolistthelast5conversationsofauser,butthequeryisverycomplex,I'mtryingtoresolvewithsubqueries,butIdidnotcomeupwithanything,itshouldmeetthefollowingrequirements:

  • Theuserinquestioncanbebothasender(sender_id)andarecipient(recipient_id),sohehastofetchboth,andwhentheuserissender_id,recipient_idmustreceiveaaliastobeauser_id(Seecodebelow);
  • Resultsshouldbesortedin%with%;
  • Youshouldreturnthelastmessageoftheconversation;
  • Themessagecannotbedeletedfortheuserinquestion(Iusedboolean0fornoand1foryes).

Thecodes:

-Wherethemessagesareready:

<?phpforeach($list_messagesas$msg):?><?php$recipient=find_user($con,$msg['user_id']);?><ahref="#" onclick="openPanelChat(<?php echo $recipient['user_id'] ?>)" class="dropdown-notification-content" style="background-color: <?php echo ($msg['seen'] == 0) ? 'rgba(28,146,243,0.08)' : 'white' ?>">
            <img src="<?php echo $recipient['picture'] ?>">
            <b><?php echo $recipient['name'] ?></b>
            <p><?php echo base64_decode($msg['msg']) ?></p>
            <span><?php echo translateDateFull($msg['registry']); ?></span>
        </a>
<?php endforeach; ?>

-A function that lists and returns everything in an array

function list_messages($con, $user_id){
    $list_sql = "Essa é a consulta que quero!";
    $r = mysqli_query($con, $list_sql) or die(mysqli_error());
    $messages = array();
    while($each = mysqli_fetch_assoc($r)){
        $messages[] = $each;
    }
    return $messages;
}

Example of how a conversation goes between id = 27 and id = 31. This is a conversation, let's assume that this id = 27 is "Arthur" and 31 is "Victory", I want to display the last message of our conversation, but I can be both sender_id and recipient_id: Well, that's it, the result should look something like facebook!

    
asked by anonymous 02.10.2016 / 03:19

0 answers