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 > Cross Join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-10, 20:14
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Cross Join

I have a more complex requirement of the Cartesian product...
I’m familiar with CROSS JOIN.

E.g. I want to have all the possible combinations of 2-student groups for a project

Table_A – Grade, Class, Name
1, 1A, John
1, 1A, Mike
1, 1B, Sam
2, 2A, Pete
2, 2B, Paul

SELECT A.Name, B.Name
FROM Table_A as A
CROSS JOIN TABLE_B as B


Result:
John, Mike
John, Sam
John, Pete
John, Paul
Mike, Sam
Mike, Pete
Mike, Paul
Sam, Pete
Sam, Paul
Etc..

***

So here is what I want. Rather than pair up ALL students, i’d like to only pair up the ones in the same class.

Required result:
John, Mike

This is the only record being returned since it is the only pair that are in the same class (1A).

Can I use CROSS JOIN to do this?


Thanks
Reply With Quote
  #2 (permalink)  
Old 02-25-10, 23:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you can do it either with a CROSS JOIN --
Code:
SELECT a.name
     , b.name
  FROM Table_A AS a
CROSS 
  JOIN Table_A AS b
 WHERE a.class = b.class
or with an INNER JOIN --
Code:
SELECT a.name
     , b.name
  FROM Table_A AS a
INNER 
  JOIN Table_A AS b
    ON a.class = b.class
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-27-10, 10:36
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
ive included a.name != b.name to avoid having students paired with themselves e.g. John, John

SELECT a.name, b.name
FROM Table_A AS a
CROSS JOIN Table_A AS b
WHERE a.class = b.class
and a.name != b.name


result:
Mike, John
John, Mike

is there an efficient way to have the query as a combination rather than permutation i.e. Mike, John is the same as John, Mike so the desired result is simply:
Mike, John

thanks again

Last edited by Diesel Dud; 02-27-10 at 11:18.
Reply With Quote
  #4 (permalink)  
Old 02-27-10, 12:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Diesel Dud View Post
is there an efficient way to have the query as a combination rather than permutation i.e. Mike, John is the same as John, Mike
yup --
Code:
SELECT a.name
     , b.name
  FROM Table_A AS a
CROSS
  JOIN Table_A AS b
 WHERE a.class = b.class
   AND a.name < b.name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-27-10, 13:09
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
awesome, thanks for the help and the prompt reply
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