3
I’m making a system of ticket
where I have the message table of each ticket
. This message can be sent by both an administrator and the user who opened the ticket
.
With this, I have the problem when getting the user name, be it an administrator or the user who opened the ticket
, because your 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 didn’t work, but the idea is that I select the nome
or the table tabela_admin
or tabela_empresa
in accordance with the usuario_tipo
.
The second option, would be to save in the ticket_message table or the admin or company id, 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 is working and, as this is not my area, I don’t know how to proceed or decide which of the options is the best to get this result and correct them.
If there is another way, something simpler or another method that is correct, there is no problem in changing. The important thing is to obtain the final result, which would obtain the name of the respective "user", be it administrator or user.
Which comic is using?
– Ricardo
@Ricardo I’m using mysql
– celsomtrindade
I edited my reply and put a link to the Fiddler running with if.
– Ricardo