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");
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**..
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;
Question: How to bring the data of each table into a while? I got quite confused
– Pedro Quezado
I’m really bad with INNER JOIN... ;( http://i.imgur.com/Jpevpbm.png @rocmartins
– Pedro Quezado
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
– Pedro Quezado
@Pedroquezado In which table is photo, username and phone?
– user6476
TABLE user >> id, username, name, surname, photo, phone @rocmartins
– Pedro Quezado
Reply edited @Pedroquezado
– user6476
This time do not replace http://i.imgur.com/viMKq2R.png @rocmartins
– Pedro Quezado
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.
– user6476
I tested with
WHERE u.id = f.user2_id GROUP BY c.id_de
and took... > http://i.imgur.com/Qwpcq0o.png– Pedro Quezado
Beauty. Answer edited with your corrections.
– user6476
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 theGROUP 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??– Pedro Quezado
@Pedroquezado, alterei o group by para o id do usuário 2, check out.
– user6476