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.

 
Go Back  dBforums > Database Server Software > MySQL > Double sided JOIN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-21-06, 09:00
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Post Double sided JOIN

Lets say we have 2 tables

Code:
+---------+-----------+---------+
| user_id | user_name | role_id |
+---------+-----------+---------+
|       1 | Peter     |       3 |
|       2 | Paul      |       1 |
|       3 | Jake      |       4 |
+---------+-----------+---------+

and

+---------+-----------+
| role_id | role_name |
+---------+-----------+
|       1 | user      |
|       2 | moderator |
|       3 | root      |
+---------+-----------+
I'd like to JOIN them by their 2 sides in order to get something like

Code:
+-----------+-----------+
| user_name | role_name |
+-----------+-----------+
| Peter     | root      |
| Paul      | user      |
| Jake      | NULL      |
| NULL      | moderator |
+-----------+-----------+
By doing something like

Code:
SELECT
 t1.user_name AS "user_name",
 t2.role_name AS "role_name"
FROM
 t1 DOUBLE_LEFT JOIN t2
  ON (t1.role_id = t2.role_id)
Is it possible or is it totally illogical (and why if its the case ?)
Reply With Quote
  #2 (permalink)  
Old 03-22-06, 11:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
what you have described is a FULL OUTER JOIN -- rows from either table, with or without matching row from the other table

unfortunately mysql does not directly support FULL OUTER JOIN

you can achieve the same result as follows:
Code:
select t1.user_name as "user_name"
     , t2.role_name as "role_name"
  from t1 
left outer
  join t2
    on t2.role_id = t1.role_id
union all    
select null         as "user_name"
     , t2.role_name as "role_name"
  from t2 
left outer
  join t1
    on t1.role_id = t2.role_id
 where t1.role_id is null
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 03-23-06 at 06:41.
Reply With Quote
  #3 (permalink)  
Old 03-23-06, 03:43
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Ok thanks
FULL OUTER JOIN is SQL ?
Reply With Quote
  #4 (permalink)  
Old 03-23-06, 06:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
yes, FULL OUTER JOIN is SQL

mysql doesn't support it, other database systems do
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On