3
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 am listing the users who both follow and who brings the latest message according to the table chat:
$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 put it GROUP BY u.id
, where it picks up all messages from a user and plays in a single box:
however it does not sort according to the most recent messages in the chat table.
Now if I take the GROUP BY u.id
it lists really according to the most recent, but it plays all messages without caring of repetitions:
How can I resolve?? Make it actually list most recently on the table chat and not with repetitions in a single div
?
the prints code make it very difficult to understand your question, if you create a fiddle SQL with its database structure and query, and adding the link in the question text will facilitate that users can give a good answer
– Pedro Sanção
@Sanction http://sqlfiddle.com/#! 9/39f0e/3/0 is this...
– Pedro Quezado
The date field is of the date type?
– KaduAmaral
@Stoneif your Sqlfiddle is not working here... The link is correct?
– KaduAmaral
You are @Kaduamaral
– Pedro Quezado