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 users who both follow and who brings the most recent message according to the chat table:
$sqln = mysqli_query($conn,"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.id_de = '$id' and c.id_para = f.user2_id) or (c.id_de = f.user2_id and c.id_para = '$id'))
WHERE
u.id = f.user2_id
GROUP BY
u.id
ORDER BY
c.data DESC, c.hora DESC");
But if I by GROUP BY u.id
, where it takes all the messages of a user and plays in a single box:
butitdoesnotsortaccordingtothelatestmemosinthechattable.
NowifItakeoutGROUPBYu.id
itactuallylistaccordingtothelatestones,butitthrowsallthemessageswithoutwantingtoknowofrepetitions:
How can I solve it ?? Do you really list the most recent table in the chat table and not repeat with a single div
?