0
In my database I have a generic entity "user", such entity is specialized in two other "store" and "service_provider", this specialization has total and exclusive characteristic, so that, an occurrence of "user" must be associated with at least one specialised entity and only one "store" or "service_provider type".
The "type" field references to which specialization the occurrence of "User" belongs, type = '1'(store) and type='2'(service_provider).
Note that the table "store" has the field "company_name" that says which the company name and the table "service_provider" has the "name" referring to the physical person’s name.
DOUBT: in a query, it is possible to collect the values of an occurrence of "user", check which specialization this occurrence belongs (by the field "type"), and if it is a "store" give a JOIN with the field "company_name" or "name" if it is a "service_provider"?.
I thought that way (pseudo code):
SELECIONAR LINHA DE user ONDE id_user = ?
INNER JOIN
IF ((SELECIONAR type DE user ONDE id_user = ? ) = '1',
SELECIONAR company_name DE store ONDE id_user = ?,
SELECIONAR name DE service_provider ONDE id_user = ?
)
I made some changes to the JOIN part, it was like this
INNER JOIN store ON store.id_user = user.id_user
 INNER JOIN service_provider ON service_provider.id_user = user.id_user WHERE user.id_user = 23
. I removed the user.type = x part, because as the primary key of the specialized entities are also part of the generic entity’s foreign key, if there is an id_user = 10 in a specialized entity for example, that id will not appear in the other specialization. But still it is not returning the Records correct, true, is not returning anything.– shelldude
If either you have one specialization or the other, but not both simultaneously, then surely the INNER JOIN you modified will return zero occurrences. That’s why Diego Marques used LEFT JOIN.
– anonimo
Yes yes, apparently I misinterpreted the use of JOINS in a data recovery, LEFT JOIN will always return the value of the table to the left even if there is no corresponding foreign key, but to solve this just put a WHERE clause at the end. I made some interpretation errors, I’m still deepening in SQL...
– shelldude