LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 07:51 PM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
Joining three tables using left and normal join


Hi,

I'm trying to join three tables using a left join and a normal join. I know it is possible to achieve that using php by executing several queries, but I don't want this process be that expensive, so I'm trying to do the whole thing in one query.

Here is the useful information about my database schema:

table 1: matchResult
columns: match_id,team1,team2,status

table 2: teams
columns: team_id,team_name

table 3: predicts
columns: match_id,user_id

What I need is to return a list of matches whose status equals to two and join records from predicts table based on match_id and those whose user_id either equals to 2 or is null.

To achieve that I use the following query:


Code:
select matchResult.match_id,matchResult.team1,matchResult.team2,predicts.user_id from 
matchResult LEFT join predicts using (match_id)
 where matchResult.status=2 and (predicts.user_id=2 || predicts.user_id IS NULL);

This query returns the following information:

PHP Code:
+----------+-------+-------+---------+
match_id team1 team2 user_id |
+----------+-------+-------+---------+
|        
|    92 |    94 |       
|        
|   104 |    98 |    NULL 
|        
|   105 |   107 |    NULL 
|        
|   109 |    96 |    NULL 
|       
10 |   111 |   103 |    NULL 
|       
11 |    95 |    97 |    NULL 
|       
12 |   101 |   110 |    NULL 
|       
13 |    93 |   108 |    NULL 
+----------+-------+-------+---------+
8 rows in set (0.00 sec
So far everything looks good, but I need to output the team names with their along side team IDs.

As you can see, the teams table contains a list of teams and their corresponding IDs. matchResult table has tow foreign keys (team1 and team2) who refer to the team_id of teams table.

I would like to know how I can join teams table to the existing query so that the output looks something like this:

PHP Code:
+----------+-------+-------+---------+-----------+-----------+
match_id team1 team2 user_id |  team1    |    team2  |
+----------+-------+-------+---------+-----------+-----------+
|        
|    92 |    94 |       Man U     |  Chelsea  |
|        
|   104 |    98 |    NULL Arsenal   |  Liverpool|
|        
|   105 |   107 |    NULL 
|        
|   109 |    96 |    NULL | and so on
|       10 |   111 |   103 |    NULL 
|       
11 |    95 |    97 |    NULL 
|       
12 |   101 |   110 |    NULL 
|       
13 |    93 |   108 |    NULL 
+----------+-------+-------+---------+
8 rows in set (0.00 sec
I have never had experience of executing such complex queries, so I would be glad if you help me with that.

Thanks

Last edited by mohtasham1983; 07-23-2008 at 07:52 PM.
 
Old 07-23-2008, 09:49 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Usually I try my queries by successive approximation until they work, so it is a bit difficult without the actual tables. Let me try, double check for silly syntax errors.

Code:
SELECT matchResult.match_id, team1, team2, user_id, teams1.team_name as tn1, teams2.team_name as tn2 from 
matchResult LEFT JOIN predicts ON matchResult.match_id = predicts.match_id
LEFT JOIN teams as teams1 on teams1.team_id = matchResult.team1
LEFT JOIN teams as teams2 on teams2.team_id = matchResult.team2
WHERE matchResult.status=2 AND (predicts.user_id=2 || ISNULL (predicts.user_id));
The trick is in selecting TWO records from teams, one matching matchResult.team1 and the other one as matchResult.team2. Since you cannot select a record from the same table using two different ON clauses, you alias the table (teams as teams2) and you use two aliases for the two ON clauses.

The other trick is teams1.team_name as tn1 where you alias the column. Both colums must have a different name otherwise you cannot distiguish them in your PHP array.

teams1.team_name refers to the alias you create in the JOIN clause.

If it doesn't work, try to replace the LEFT JOIN with plain JOIN here and there

If it still doesn't work let me know, I would have to create the tables to try further. But it is certainly possible, I do it all the time. (Iteratively, that is!)

Just a hint, rename your matchResult.team1 to matchResult.team1_id. It is an id actually, and it is easier remind you of that.

jlinkels
 
Old 07-23-2008, 10:05 PM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Excellent help. You taught me a new trick. By the way, you guessed right that I needed to create alias for team. since I was generating an xml document based on column names of the database by using mysql_fetch_assoc, i was having trouble at first. But your trick took care of everything.

Thank you very much.

Last edited by mohtasham1983; 07-23-2008 at 10:09 PM.
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scroll left goes right and right goes left adamruss Linux - Hardware 2 08-17-2007 11:31 AM
Unable to join domain using Net Join command in FC3 client jeb083079 Linux - Networking 9 07-30-2007 02:41 AM
LXer: Learn how to join tables in MySQL LXer Syndicated Linux News 0 08-08-2006 03:21 PM
Help using 'net join' to join a windows domain Wapo Linux - Networking 1 04-28-2006 02:30 AM
SQL query and left join order Kostko Programming 1 04-24-2004 05:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration