I need to select some data from a single table depending on the type of user, for example, an Individual user will have the RG and PIS field while a user will have the state Registration and Fancy name field.
I can execute a similar query with LEFT JOIN
, however all fields are selected, differentiating only from having the field filled or null
, according to the type of user. What I want is to select fields only when the type is matched, eg
- Type 1 or 2: Select common fields;
- Type = 1: Selects the Individual data (filled in or not);
- Type = 2: Select the Corporate Data (filled in or not);
The query I currently have would look something 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 would like them not to appear, is it possible?