Page 1 of 1

Multiple JOIN's

Posted: Thu Oct 23, 2008 9:33 pm
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

Re: Multiple JOIN's

Posted: Fri Oct 24, 2008 5:17 am
by Oren

Re: Multiple JOIN's

Posted: Fri Oct 24, 2008 6:40 am
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.