Mysql, SELECT according to the specialisation of the generic entity occurrence

Asked

Viewed 157 times

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".inserir a descrição da imagem aqui

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 = ?
 )

1 answer

2


It is possible, and there are some approaches that may vary depending on your DBMS. The most comprehensive would be something like this:

SELECT
    user.id_user,
    user.type,
    (
        CASE user.type
            WHEN 1 THEN store.company_name
            WHEN 2 THEN service_provider.name
        END
    ) AS name
FROM
    user
    LEFT JOIN store ON user.id_user = store.id_user AND user.type = 1
    LEFT JOIN service_provider ON user.id_user = service_provider.id_user AND user.type = 2

Explaining:

We select all user records doing LEFT JOIN with both tables filtering by type, with this we will have the JOIN successfully for the record that exists. And SELECT we make a CASE to identify and return the table/field value according to type.

  • 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.

  • 1

    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.

  • 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...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.