faqts : Computers : Databases : MySQL : Language and Syntax : Queries : Joins

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

37 of 74 people (50%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I do a JOIN statement?
How do I handle join statements where the same column name is in multiple tables?

Jan 23rd, 2000 19:05
Nathan Wallace, Ville Mattila, Richard Lynch


A JOIN defines the relationship between two (or more!) tables.  The
basic things you need to supply are the two tables and a "where " clause
defining how the two tables relate.

So, if we have two tables:

    FLIGHTS:
      Fields: Airline, Flight Number

    AIRLINES:
      Fields: Code, Name

Assuming "Name" in AIRLINES is the same as "Airline" in FLIGHTS:

    select ... 
    from FLIGHTS, AIRLINES
    where FLIGHTS.Airline = AIRLINES.name

Now, for the ... part, you put the columns you want.  Sometimes, though,
you can have *TWO* columns in different tables with the same name.  (You
don't here, but you could have.)  When you need to "fully qualifiy" a
column, you add the table and a period in front, just like in the where
clause above.  So I would write this as:

    select AIRLINES.Code, AIRLINES.Name, 'FLIGHTS.Flight Number' ...