Author Topic: Mulitple INNER JOIN's  (Read 1050 times)

0 Members and 1 Guest are viewing this topic.

Offline penguTopic starter

  • Enthusiast
  • Posts: 154
    • View Profile
Mulitple INNER JOIN's
« on: January 21, 2010, 06:46:20 PM »
Hello all, haven't posted for a while.

I did a quick search on this and found it for MySQL.

So I want to do something like..

Code: [Select]

SELECT table1.id, table1.info, table2.description, table3.example FROM table1 INNER JOIN table2 ON table1.id = table2.id


That much works, but not sure how to or if it is even possible to do another INNER JOIN.

Thank you for help!
$pengu="cool";

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Mulitple INNER JOIN's
« Reply #1 on: January 21, 2010, 06:52:41 PM »
Yes, it is possible to have multiple JOINS in one query. Just make sure your syntax is correct.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline penguTopic starter

  • Enthusiast
  • Posts: 154
    • View Profile
Re: Mulitple INNER JOIN's
« Reply #2 on: January 21, 2010, 07:26:39 PM »
Could you give me a little example of the correct syntax?
$pengu="cool";

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Mulitple INNER JOIN's
« Reply #3 on: January 21, 2010, 11:46:03 PM »
Code: [Select]
SELECT
  t1.id, t1.info,
  t2.description,
  t3.example
FROM
  table1 AS t1
INNER JOIN
  table2 AS t2
ON
  t1.id = t2.id
INNER JOIN
  table3 AS t3
ON
  t2.id = t3.id
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline penguTopic starter

  • Enthusiast
  • Posts: 154
    • View Profile
Re: Mulitple INNER JOIN's
« Reply #4 on: January 22, 2010, 12:23:48 AM »
Bah.. this is what I did, the 'AS' is not needed, right?  It's just a mask for table name sort of thing?

Thanks for response anyways.
$pengu="cool";

Offline gizmola

  • Administrator
  • Freak!
  • *
  • Posts: 5,053
  • Gender: Male
  • Let's go Flyers
    • View Profile
    • GizmoLA.com
Re: Mulitple INNER JOIN's
« Reply #5 on: January 22, 2010, 12:31:29 AM »
Bah.. this is what I did, the 'AS' is not needed, right?  It's just a mask for table name sort of thing?

Thanks for response anyways.

Yes it's optional, it aliases the table, so that you don't have to spell out the full table name all the time.  There are also times when you require an alias -- namely when you are joining a table to itself.

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Mulitple INNER JOIN's
« Reply #6 on: January 22, 2010, 01:05:56 AM »
Also if you're joining a subquery rather than actual table, you have to give it an alias.

Code: [Select]
SELECT
  t1.ID, t1.field1,
  sq.field2, sq.field3
FROM
  table1 AS t1
INNER JOIN (
  SELECT
     ID, field2, field3
  FROM
    someOtherTable
  WHERE
    someCondition = TRUE
) AS sq
ON
  t1.ID = sq.ID
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline penguTopic starter

  • Enthusiast
  • Posts: 154
    • View Profile
Re: Mulitple INNER JOIN's
« Reply #7 on: January 24, 2010, 10:56:04 PM »
Thanks for the help guys.

Found the problem, I was editing a report created by someone else and some of the field names didn't have the table prefix at the front and it was clashing with the other tables.  All good now!

Solved.
$pengu="cool";