Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > MySQL Join question

MySQL Join question
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2004
Location: Claremont, CA
Status: Offline
Reply With Quote
Nov 13, 2004, 06:07 PM
 
Hi there,

I have a database with roughly 15 tables, 10 of which each have a person_id. I want to bring back all the information from each table that corresponds to a specific person_id. I've found that I can do:

SELECT * FROM table1 LEFT JOIN table2 USING(person_id) LEFT JOIN table3 USING (person_id) LEFT JOIN table3 USING(person_id)......WHERE table1.person_id = X


Is there any shorter way for me to code this without having to continually repeat the 'USING' command over and over? Maybe even avoid using the LEFT JOIN command over and over.....

Any help is greatly appreciate, thanks in advance!
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Nov 15, 2004, 12:43 PM
 
SQL is not known to be a terse language... this is just a part of using it.
     
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status: Offline
Reply With Quote
Nov 15, 2004, 12:49 PM
 
A 10-table inner join? Wow. I hope performance doesn't matter in your application.

As for syntax, I'm not sure, but there might be some MySQL-only syntax that might make this easier, but then it wouldn't be compatible with any other database. I would not recommend this in case you ever have to move from MySQL to something else.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Nov 15, 2004, 01:03 PM
 
Originally posted by SineWaveMaster:
Is there any shorter way for me to code this without having to continually repeat the 'USING' command over and over? Maybe even avoid using the LEFT JOIN command over and over.....
What about something like this:

Code:
SELECT table1.person_id, table2.person_name,table3.foobar, table4.schmoo FROM table1, table2, table3, table4 WHERE table1.person_id = 'something' AND table1.person_id = table2.person_id AND table1.person_id = table3.person_id AND table1.person_id = table4.person_id
As a general rule I never use "SELECT * FROM", instead specifying the fields from the tables I want to pull in.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Nov 15, 2004, 05:03 PM
 
If this were a better database than MySQL, such as PostreSQL then you could create a view to take care of both the hairy syntax and alleviate some of the performance penalty... but...
     
Mac Elite
Join Date: Mar 2001
Location: CO
Status: Offline
Reply With Quote
Nov 17, 2004, 08:34 AM
 
larkost...

Programming does not come easily to me, so I have just used MySQL for simple db use on my web site (and ease of support on whatever hosting service I'm on).

How is the learning curve to switch to PostreSQL (for a PHP front-end)? the universality of support at Hosts? The additional power to be gained?
TOMBSTONE: "He's trashed his last preferences"
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Nov 17, 2004, 10:39 AM
 
As far as switching the SQL... PostreSQL supports more of the SQL standard than MySQL. As far as I know there is nothing that MySQL does that PostreSQL does not do (the reverse is definitely not true). So you can add whatever you want out of the available SQL.

On the admin side, there is the annoyance of having to periodically "vacuum" the db on PostgreSQL, and there are some differences in the security model, but since most people don't even know about the security model on MySQL... The vacuuming is probably the reason that more hosts don't provide PostgreSQL. More recently this has become less of a problem, but people have not caught on yet.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2