Multiple JOIN's

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Multiple JOIN's

Post by alex.barylski »

Trying to understand JOIN's I'm wondering how I would perform JOIN's on more than two tables, currently O have the following:

[sql]SELECT * FROM users JOIN groups ON users.grid = groups.pkid[/sql]

Hoopefully what I have asked made sense. :P
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Multiple JOIN's

Post by Oren »

User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Multiple JOIN's

Post by onion2k »

You can join as many tables as you like... Here's a typical example from my store app..

Code: Select all

SELECT ...
FROM `acu_cart` 
INNER JOIN `acu_country` sc ON sc.`country_id` = `acu_cart`.`shipping_country_id` 
INNER JOIN `acu_country` cc ON cc.`country_id` = `acu_cart`.`card_country_id` 
LEFT JOIN `acu_creditcard_type` ct ON ct.`creditcard_type_id` = `acu_cart`.`card_type_id` 
LEFT JOIN `acu_user` u ON u.`user_id` = `acu_cart`.`user_id` 
...
There's actually a lot more than that (9 joins in fact). If you're going to be doing lots of them make sure you've got indexes defined for tables that'll have loads of records if you're not joining on the primary key otherwise it'll be hellishly slow.
Post Reply