query with columns of several tables using inner join

0

I have a table stock which brings me (among other things) the id of other tables, what I need is to make a query that instead of bringing the ID of the other tables, bring the name of the corresponding element to said Id. The diagram of the BD is as follows

and the consultations carried out are the following:

The arrows indicate where each result should go, in the case of id_element , this should bring element , which in turn has a id_elementBase , which has the name that I need to show. I tried using inner join but I did not manage to make it work correctly, and using and the result is incorrect. Thanks for your help!

    
asked by sebastian 03.12.2018 в 22:24
source

3 answers

1

You must do a join of the different tables, to get the > related data in the columns.

The basic idea is to explore it between the tables stock and provider , where we will get the name of the provider for each record of stock , it would be:

 select   st.id_stock
        , pr.name provider_name
        , st.lot
        , st.expire_date
        --etc...
   from stock st
        left join provider pr on pr.id_provider = st.id_provider

I used a left join , since I do not know if the field is not null , so that all of them appear the stock records regardless of whether there is a row in the provider table.

In the clause on of join I indicated to the engine the fields for which a table is related to the other.

I have also used an alias for the column pr.name , so that it is clear what is the data that is there.

Expanding this idea, you can add the other tables, the from, with 4 of them it would look something like:

   from stock st
        left join provider pr on pr.id_provider = st.id_provider
        left join base ba on ba.id_base = st.id_base
        left join element el 
                  left join elementModel emo on emo.id_elementModel = el.id_elementModel
               on el.id_element = st.id_element
    
answered by 03.12.2018 в 22:36
1

Try this:

What is being done is to put an alias to the column of each table, we are going to bring it from stock where the id matches what is in stock . \ If you want to bring one in specific you indicate the where .

select b.name as baseName,p.name as providerName, e.elementModel as element, s.creation_date,s.id_provider,s.id_element,s.lot,s.expireDate,s.id_base,s.entryDate as departureDate,s.residue 
from stock as s
        inner join base as b on id_base = s.id_base
        inner join providerName as p on id_provider = p.id_provider
        inner join element as e on id_element = s.id_element 
        where s.id_stock = 1

If you do not like it, and you want to bring it all, just skip it.

select b.name as baseName,p.name as providerName, e.elementModel as element, s.creation_date,s.id_provider,s.id_element,s.lot,s.expireDate,s.id_base,s.entryDate as departureDate,s.residue 
    from stock as s
            inner join base as b on id_base = s.id_base
            inner join providerName as p on id_provider = p.id_provider
            inner join element as e on id_element = s.id_element 
    
answered by 03.12.2018 в 22:35
1

Using Joins podrias:

With the join you make the union between the tables, this union you do it by means of the id that they have in common or they are foreign keys, when you choose the data that you want to show you put a suffix that will be after the alias that will have the example table if you want to bring the product name you put p.name and when you call the product table in the from you add the alias from product p that way you are specifying that you bring the name field of the product table (in case you have that field name repeated in several tables)

    SELECT s.creation_date as date, p.name as productName, em.name as elementName, 
    s.lot, s.expireDate,
    b.name, s.entryDate as departureDate, s.residue
    from stock s inner join provider p on p.id_provider = s.id_provider
    inner join base b on b.id_base = s.id_base
    inner join element e on e.id_element = s.id_element
    inner join elementModel em on em.id_elementModel = e.id_elementModel
    
answered by 03.12.2018 в 22:40