1
I need to select some data from a single table depending on the type of user, for example, a Personal user will have the field RG and PIS while a user will have the field State registration and fancy name.
I can run a query similar to LEFT JOIN
, but all fields are selected, differing only from having the field filled or null
, according to the type of user. What I want is to select the fields only when the type is corresponding, eg.:
- Type 1 or 2: Selects common fields;
- Type = 1: Selects Natural Person data (completed or not);
- Type = 2: Selects Legal Entity data (completed or not);
To query that I currently have would be more or less like this:
SELECT
a.campo_a, a.campo_b, a.campo_c,
-- Tipo 1
b.campo_d, b.campo_e, --pode ter mais campos aqui
-- Tipo 2
c.campo_f, c.campo_g --pode ter mais campos aqui
FROM tabela AS a
LEFT JOIN tabela AS b ON a.tipo = 1
LEFT JOIN tabela AS c ON a.tipo = 2
WHERE a.id = :id
However, if I am selecting type 1, I still see the fields campo_f
and campo_g
, I wish they wouldn’t even show up, it’s possible?
having as example individual/legal person, in the same table you have 2 columns, one for CPF and the other for correct CNPJ ? It has many columns so ?
– Rovann Linhalis