SQL with SELECT and SUBSELECT to mount a chat

Asked

Viewed 61 times

0

Hello guys from Stackoverflow, I’m mounting the SQL below to mount a chat service user, which brings the list of all active conversations, but I’m having a problem, my SQL is bringing the latest message according to the Customer id, or if my message is last, it does not display, and I need to put a COUNT if there are new messages if the data_message is > that the data_message(current) for me bring notifications of how many messages are.

OBS: Table that stores messages: http://prntscr.com/jmkde8

SELECT CLI.id, CLI.nome, CLI.senha, CLI.email, CLI.cpf, CLI.celular, CLI.data_nasc, CLI.genero, CLI.data_cadastro, CLI.status, CLI.id_socket, ATEN.mensagem, COUNT(ATEN.mensagem) AS novas_mensagens, SUM(COMP.valor) AS valor_total, COMP.data AS ultima_compra, ARQ.nome AS foto, ATEN.data_mensagem
FROM ut_clientes AS CLI

LEFT JOIN ut_compras AS COMP ON COMP.id_cliente = CLI.id
LEFT JOIN ut_arquivos AS ARQ ON ARQ.id_tipo = CLI.id AND ARQ.tipo = 'ut_clientes'
LEFT JOIN ut_atendimentos AS ATEN ON ATEN.id_usuario_envio = CLI.id

WHERE ATEN.id_usuario_envio
  AND NOT EXISTS(
    SELECT ATEN.id_usuario_envio
    FROM ut_atendimentos AS ATEN2
    WHERE ATEN2.id_usuario_envio = CLI.id
    AND ATEN2.data_mensagem > ATEN.data_mensagem
  )
GROUP BY CLI.id
ORDER BY ATEN.data_mensagem DESC

I do not know if the idea was clear, but anything just ask me, I would appreciate if someone could give a light kkk

Thanks!

  • 1

    It was not clear. It has how to put the contents tables and examples of what you expect?

  • There’s nothing missing from this part? WHERE ATEN.id_usuario_envio
 AND NOT EXISTS

  • Of course, I think with the front end it will be simpler, so this is how it’s being returned now: http://prntscr.com/jmkkpq, you can see that my message is not the last one on the list displayed on the client’s chatlist, the idea of COUNT, is, by not being active with the client a COUNT with the number of unread messages will be displayed (Whatsapp style)

No answers

Browser other questions tagged

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