I have 4 tables:
Saida{
id int,
idDestino int,
idProduto int,
tipoDestino int,
quant int,
data date,
solicitante varchar}
Paciente{
id int,
nome varchar}
Unidade{
id int,
nome varchar}
Produto{
id int,
nome varchar,
quant int}
The saida
table records the inventory items that went out for Patients or Units.
In my program, I do the following: if it does. TypeDestine equals 0, then the output was for a Patient. if destination.Type is equal to 1, then the output went to a Unit.
So far so good. What I need from you is to do a select + - like this:
SELECT
saida.id, saida.data,
(CASE saida.tipoDestino WHEN 0 THEN paciente.nome WHEN 1 THEN unidade.descricao END) as Destino,
produto.descricao, saida.quant
FROM
saida, paciente, unidade, produto
WHERE
paciente.id = saida.idDestino OR
unidade.id = saida.idDestino
I need the name of the Destino
or the Paciente
field in the Unidade
field, according to the value of the tipoDestino
field.
The result even returns values, but shows many repeated fields.