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");
Buttheonlyflawisthatitonlyliststheuserswhofollowit,andwhatIreallyneedisthatit(div
),YES,listuserswhofollowit,butmoredynamicwaybringingtheusersshefollowsbuttheoneswhotalkedtoherandTOPfortheLASTaccordingtothemostrecentmessages**..
It's basically making the search on the query
that lists the linked tables ( User and Following ) also contact the Chat as if it were a ORDER
according to the recent messages among users that the user will follow.
I saw examples like this but it did not 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;