1
I have the following tables:
_________PERSON_________
serial id PK
name varchar(50) NOT NULL
email varchar(100) NOT NULL
tel varchar(14) NOT NULL
_________CLIENT__________
integer person FK/PK --(FK to id in tbl PESSOA)
rg varchar(20) NOT NULL
Cpf varchar(14) NOT NULL
_________DEPENDENT________
integer person FK/PK --(FK to id in tbl PESSOA)
integer guarantor FK NOT NULL-(FK to person in tbl CLIENT)
authorized Boolean NOT NULL
_________STAFF________
integer person FK/PK --(FK to id in tbl PESSOA)
root Boolean NOT NULL
A person can be either an employee or a customer or a dependent or the three.
I want to make a select that tells me in which of the three tables the PERSON ID is used as a foreign key, i.e., whether a person is a client, a client/employee, a client/dependent/employee, etc.
The ideal return form can be an array of chars with the initial Lette of each table (e.g. {C, D, F}), or a string composed of the initial letter of each 'CDF'.
I could create a column tipo
of the kind char(1)[]
on the table PESSOA
so every time a person is referenced in any of the three tables, the person table would have the field tipo
updated by inserting the initial letter corresponding to the table.
But if it can be solved with a select, I think it would be more practical.
See if this solves your problem: http://pastebin.com/KhkBW05G
– Sorack
It wasn’t exactly that, but it helped find the way. I don’t really need the person’s name to come back, I just really need the lyrics. I also need to make this consultation for a specific person. A clause was enough
WHERE
at the end of eachJOIN
. Thus, if, for example, the person with ID 1 is referenced in the three tables (Client, Dependent, Employee) the query will return three lines ('C', ’D', 'F'). If it is only in Customer and Employee, the query will return two lines ('C', 'F'). So on. If the person is not in any, the return is empty.– Matheus Saraiva
If you want to do with subselect, then you will return a row with only 3 columns
– Sorack
How would it be with subselect?
– Matheus Saraiva