Query of multiple Mysql data

Asked

Viewed 50 times

0

I’m doing a chat and I have the following table in the bank:

inserir a descrição da imagem aqui

I want to list the last 5 conversations of a user, but the query is very complex, I’m for a while trying to solve with Queries, but I didn’t get anything, it should meet the following requirements:

  • The user in question can be both a sender (sender_id) as a recipient (recipient_id), then you have to fetch both, and when the user is sender_id, the recipient_id has to receive a alias to be a user_id (See the code below);
  • Results should be ordered in DESC;
  • You must return the last message of the conversation;
  • The message cannot be deleted for the user in question (I used as Boolean 0 for no and 1 for yes).

The codes:

-Where I list the messages:

<?php foreach($list_messages as $msg) : ?>
        <?php $recipient = find_user($con, $msg['user_id']); ?>
        <a href="#" 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; ?>

-The 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 a conversation between id=27 and id=31. This is a conversation, let’s assume that this id=27 is me "Artur" and 31 is "Vitória", I want to display the last message of our conversation, but I (id=27) can be both sender_id and recipient_id: inserir a descrição da imagem aqui Well, that’s it, the result should be something like the one on facebook!

  • you could put an example of how the table is with data and how it would have to look, it improves the interpretation!

  • That’s better now?

  • It’s a little complex, can help?

  • select * from tabela where (sender_id=27 or recipient_id=27) ORDER BY registry desc LIMIT 1 not only that ?

  • No, you have to return id=37 as user_id, and has to be the last 5 conversations (no messages).

  • I was finally able to associate the user with sender_id or recipient_id, but it should come differently (without duplication), considering that regardless of the number of messages you send or receive, it is just a conversation. I tried using the DISTINCT clause (SELECT DISTINCT IF(sender_id = 27, recipient_id, sender_id) as user_id, msg, registry, seen FROM messages_chat WHERE recipient_id = 27 OR sender_id = 27 ORDER BY id DESC limit 5) unsuccessfully because I need several columns (I tried using sub-consultations, but it got worse more)...

  • Another solution was to use GROUP BY (SELECT IF(sender_id = 27, recipient_id, sender_id) as user_id, msg, registry, seen FROM messages_chat WHERE recipient_id = 27 OR sender_id = 27 GROUP BY user_id ORDER BY id DESC limit 5), but the main problem is that it does not order in the order I am specifying, precisely because GROUP BY does its work first.

  • With Group By it brings the 5 messages correctly?

  • It brings 3 (which is as much as there is). The problem is that it brings and only after it runs ORDER BY (according to the results of GROUP BY). It brings the distinct id as it is to be, but does not bring in order!

  • My friend is simple to put the order then! SELECT * FROM (SELECT IF(sender_id = 27, recipient_id, sender_id) as user_id, msg, registry, seen FROM messages_chat WHERE recipient_id = 27 OR sender_id = 27 GROUP BY user_id ORDER BY id DESC limit 5) AS tabela2 ORDER BY CAMPO_DE_ORDENAÇÃO

  • I don’t think you understand. In the query I sent (which you put in the subquery) it returns grouped in random order, when ORDER BY comes soon after, it works on top of this random order, which does not help. What I want is to do the same as Facebook, do you have an account? It takes the last messages and then groups. I even tried to do this: SELECT * FROM (SELECT IF(sender_id = 27, recipient_id, sender_id) as user_id, msg, registry, seen FROM messages_chat WHERE recipient_id = 27 OR sender_id = 27 ORDER BY id DESC) AS tabela GROUP BY user_id limit 5, but remains in random order.

  • http://forum.imasters.com.br/topic/553073-multiplos-dados-na-consulta/#entry2207201

  • Motta, here developed a lot, what you sent there does not match very well with the problem, can help here?

  • 1

    I haven’t quite figured it out yet!

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.