Query to multiple tables in mysql

Asked

Viewed 266 times

1

My model is the following, I have 4 tables

Avião (tailnum (PK), year)
Voos (id_voo (PK), cancelled)
Modelo (idmodel (PK), nome)
Fabricante (id_fabricante (PK), nome)

Airplane is with a 1:M connection with the flight chart and model, and the model table is with a 1:M connection with manufacturer. I intend to make a query of the genre "Which Boeing aircraft built before the year 2000 that had canceled flights". Boeing is the name of the manufacturer, and in relation to flights canceled on the flight table is a boolean and that when the value = 1 the flight was canceled. Then the year is on the plane table.

I’m doing it this way:

Select tailnum
From
      (Select t.tailnum
       From Aviao t
       Inner Join Modelo M
       ON t.tailnum = M.tailnum
       Inner Join Fabricante C
       ON M.idmodel = C.idmodel
       Where nome = "Boeing"
       group by t.tailnum )
Where year < 2000 and tailnum IN (Select tailnum
                                  From voos
                                  where cancelled = 1)

In this I will get the Boeing planes below 2000, as I do to now go to the table flights and bring the cancelled = 1. Still causes me some confusion these going to various tables, will someone help me?

  • I edited the part to fetch from the flights table according to some tutorials I saw, will be correct so?

1 answer

3

I was able to perform the query as follows:

select distinct
   a.tailnum
from
   aviao a,
   voos v,
   fabricante f,
   modelo m
 where
   a.year < 2000 and
   f.nome = 'Boeing' and
   v.cancelled = 1 and
   a.tailnum = m.tailnum and
   f.id_fabricante = m.id_fabricante and
   a.tailnum = v.tailnum

See if it’s any use to you.

Sqlfiddle

@EDIT

Using Joins, which is the most recommended form:

select 
   a.tailnum
from
   aviao a
   left join voos v 
   on a.tailnum = v.tailnum
   inner join modelo m 
   on m.tailnum = v.tailnum
   inner join fabricante f 
   on f.id_fabricante = m.id_fabricante   
 where
   a.year < 2000 and
   f.nome = 'Boeing' and
   v.cancelled = 1

SQL Fiddle

  • Gives a check if joins application instead of connecting from is appliable, as it is more optimized internally in the database.

  • This is even a good question (if they haven’t done it yet). I don’t particularly see any difference in the performance issue, but I will search.

  • 1

    The point is that using joins in the correct way would not need to use distinct, applying Join (shows only corresponding record in both tables) using left Outer Join or right Outer Join (returns NULLS from the left or right table in Join even without matching)

  • Perfect. I will review using the joins.

  • I think I already got through joins, I just really need to test with the actual data, thank you :)

  • 1

    If it works put here that can help someone in the future, dear Ellipe if later tested with post joins here also to compare.

  • 1

    @Gonçalo Feito!

Show 2 more comments

Browser other questions tagged

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