PHP - Bring the result of a table according to the most recent message from another table?

Asked

Viewed 211 times

5

So.. I have 3 table:

user:
    | ID | NOME | SOBRENOME |
    | 01 | Igor | Ferreira  |
    | 02 | João | Henrique  |
    | 03 | Rose | Beltão    |

following:
    | ID | user1_id | user2_id |
    | 01 |    01    |    02    |  User 1 > Segue User 2
    | 02 |    02    |    01    |     -- User 2 > Segue User 1
    | 03 |    01    |    03    |  User 1 > Segue User 3
    | 04 |    03    |    01    |     -- User 3 > Segue User 1
    | 05 |    03    |    02    |  User 3 > Segue User 2
    | 06 |    02    |    03    |     -- User 2 > Segue User 3

chat:
    | ID | user1 | user2 |      mensagem      |       data       |
    | 01 |   02  |   03  | ola Rose.          | 30/07/2015 08:25 |  De João > Para Rose
    | 02 |   03  |   02  | Oi João, como tas? | 30/07/2015 08:28 |  De Rose > Para João
    | 03 |   02  |   03  | Estou bem, e você? | 30/07/2015 08:29 |  De João > Para Rose
    | 04 |   01  |   02  | Rose você esta ai? | 30/07/2015 09:11 |  De Igor > Para Rose


In a div I’m listing the Friends that the user follows:

$sqln = mysqli_query($conn,"SELECT * FROM users 
                            WHERE (id = '$id') 
                            OR (id IN (SELECT user2_id FROM following WHERE user1_id='$id'))
                            ORDER BY id ASC");

Div de amigos!

But the only flaw is that it only lists the users who follow it, and what I really need is that it (div), YES, list the users who follow her, but more dynamically bringing the users she follows, but the ones who talked to her and who TOP to the LAST according to the latest messages**..

Listar como WhatsApp

It is basically to make the research in the query which lists the interconnected tables (User and Following) also contact the table Chat as if he were carrying a ORDER according to recent messages between users that the user will follow.

I’ve seen examples like this but it didn’t work:

SELECT u.id, u.nome, MAX(c.data_hora) AS last_msg FROM chat c 
INNER JOIN users u ON c.id_para = u.id
WHERE c.id_de = 1
GROUP BY c.id_para ORDER BY last_msg DESC;

2 answers

3


SELECT
    u.id,
    u.foto,
    u.username,
    u.nome_us,
    u.sobrenome_us,
    u.tell
FROM users u
   RIGHT JOIN following f
      ON f.user1_id = '$id'
   INNER JOIN chat c
      ON ((c.user1 = '$id' and c.user2 = f.user2_id) or (c.user1 = f.user2_id and c.user2 = '$id'))
WHERE
   u.ID = f.user2_id
GROUP BY
   f.user2_id
ORDER BY
   c.data DESC, c.hora DESC

When executing the select above you will be bringing the columns "id", "photo", "username", etc. These column names should be used to read the obtained data:

$id_q = $num_q['id'];
$foto = $num_q['foto'];
$username = $num_q['username'];
  • Question: How to bring the data of each table into a while? I got quite confused

  • I’m really bad with INNER JOIN... ;( http://i.imgur.com/Jpevpbm.png @rocmartins

  • I think I just don’t know how to get the name, photo, user id this way kkk, pq sent another message from another user and appeared another http://i.imgur.com/dZUCyZ8.png.. so I am not able to return the data of who sent.. within the table user need to pick up, ID, PHOTO, USERNAME, NAME, SURNAME, PHONE

  • @Pedroquezado In which table is photo, username and phone?

  • TABLE user >> id, username, name, surname, photo, phone @rocmartins

  • Reply edited @Pedroquezado

  • This time do not replace http://i.imgur.com/viMKq2R.png @rocmartins

  • Probably this happens due to that if with the Ids. It’s kind of in the dark, but try now with the right Join at the beginning.

  • I tested with WHERE u.id = f.user2_id GROUP BY c.id_de and took... > http://i.imgur.com/Qwpcq0o.png

  • Beauty. Answer edited with your corrections.

  • AGAIN PROBLEM @rocmartins.. If I put the GROUP BY u.id, where it takes all messages from a user and plays in a single box, but it does not sort according to the most recent messages in the table chat >> http://i.imgur.com/Dh4yvui.png --- Now if I take the GROUP BY u.id it lists really according to the most recent ones, but it plays all messages without wanting to know repetitions: http://i.imgur.com/Glhxpel.png. How can I solve??

  • @Pedroquezado, alterei o group by para o id do usuário 2, check out.

Show 7 more comments

1

Pedro, try this query below. However, I don’t know what you need. I needed to create a temporary bank to test better.

If you want to use the Sqlfiddle to create a temporary bank and give us the LINK to help you better.

SELECT
    NOME, SOBRENOME
FROM
    USER
INNER JOIN
    FOLLOWING ON FOLLOWING.USER1_ID = USER.ID
INNER JOIN
    CHAT ON CHAT.USER2 = FOLLOWING.USER2_ID
ORDER BY 
    DATA 
DESC

Browser other questions tagged

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