I'm trying to make an inner join if a condition is true but it does not work, I've tried these 2 ways:
IF chat.tipo = 'vitima' THEN
INNER JOIN vitima ON vitima.id_vit = chat.id_tipo
ELSE
INNER JOIN terceiro ON terceiro.id_ter = chat.id_tipo
or
IF(chat.tipo = 'vitima',
INNER JOIN vitima ON vitima.id_vit = chat.id_tipo,
INNER JOIN terceiro ON terceiro.id_ter = chat.id_tipo)
But both give error, what I want is if the type equals "vitima" it does the inner noin in one table, otherwise in the other.
Full query:
SELECT ocorrencia.id_oco,
(SELECT GROUP_CONCAT(c.id_oco ORDER BY c.id_oco DESC) FROM ocorrencia as c WHERE c.id_sup_oco = ocorrencia.id_sup_oco) as grouped_ids,
ocorrencia.id_sup_oco,
chat.id_tipo,
suporte_oco.data_sup,
suporte_oco.placa_sup,
suporte_oco.sinistro_sup,
suporte_oco.prefixo_sup,
ocorrencia.id_emp_oco,
IF(chat.tipo = 'vitima', vitima.nome_vit, terceiro.nome_ter) as nome,
chat.tipo
FROM chat
INNER JOIN ocorrencia ON ocorrencia.id_oco = chat.id_oco_cha
INNER JOIN suporte_oco ON suporte_oco.id_sup = ocorrencia.id_sup_oco
IF chat.tipo = 'vitima'
INNER JOIN vitima ON vitima.id_vit = chat.id_tipo
ELSE
INNER JOIN terceiro ON terceiro.id_ter = chat.id_tipo
WHERE chat.id_user = '20' OR chat.id_user_req = '20' GROUP BY (chat.id_oco_cha) ORDER BY chat.data DESC