I'm doing a ticket
system where I have the message table for every ticket
. This message can be sent either by an administrator or by the user who opened ticket
.
With this, I have the problem of getting the user name, be it an administrator or the user who opened ticket
, because their records are in different tables ( tabela_admin
, tabela_usuario
).
The first option I tried to use was the following:
//Estrutura da tabela ticket_mensagem
id | ticket_id | usuario_id | usuario_tipo | data_criado
//outro sql obtêm os dados gerais do ticket
SELECT a.*, b.nome
FROM ticket_mensagem a
LEFT JOIN tabela_admin b ON a.usuario_id = b.id AND a.usuario_tipo = 1
LEFT JOIN tabela_usuario b ON a.usuario_id = b.id AND a.usuario_tipo = 2
WHERE a.ticket_id = '$ticket_id'
Obviously it did not work, but the idea is that I select nome
or table tabela_admin
or tabela_empresa
according to usuario_tipo
.
The second option would be to save the table ticket_message or the id of admin or company, leaving the other as null, follows example:
//Estrutura da tabela ticket_mensagem
id | ticket_id | admin_id | usuario_id | data_criado
1 | 1 | NULL | 2 | .....
2 | 1 | 4 | NULL | .....
//outro sql obtêm os dados gerais do ticket
SELECT a.*, b.nome, c.nome
FROM chamado_msg a
LEFT JOIN tabela_admin b ON a.admin_id = b.id
LEFT JOIN tabela_usuario c ON a.usuario_id = c.id
WHERE a.id_ticket = '$id_ticket'
The problem is that none of the options are working, and since this is not my area very much, I do not know how to proceed or decide which option is best to get this result and correct them.
If there is another medium, something simpler or another method that is correct, it is okay to change. The important thing is to get the final result, which would get the name of the respective "user", be it administrator or user.