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 > Intersect in MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-08-08, 10:23
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Intersect in MySQL

Hi all,

THere is no intersect statement in MySQL. I figure out a workaround as shown below. However, I am not sure if it is correct or not. Please help.

If it is correct. Then Will it perform well in terms of executing speed.
Code:
SELECT * FROM (
	SELECT DISTINCT col1 FROM t1 WHERE...
	UNION ALL
	SELECT DISTINCT col1 FROM t1 WHERE...
) AS tbl
GROUP BY tbl.col1 HAVING COUNT(*) = 2
Reply With Quote
  #2 (permalink)  
Old 11-08-08, 14:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it looks fine, assuming that the WHERE clauses are different

what are they, by the way?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-08-08, 21:20
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Quote:
Originally Posted by r937
it looks fine, assuming that the WHERE clauses are different

what are they, by the way?

I would like to get the intersection of two tables. Both of them has onle one colume, titled 'Employee_ID', the followings are example tables of the two tables.

tb1
Code:
Employee_ID
0001
0002
0003
0004
tb1
Code:
Employee_ID
0001
0002
I would like to get the employee_IDs appear in the both tables.
i.e.,
Code:
0001
0002
Reply With Quote
  #4 (permalink)  
Old 11-09-08, 00:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use an INNER JOIN

what happened to the first example?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-10-08, 00:02
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Quote:
Originally Posted by r937
use an INNER JOIN

what happened to the first example?
I have tested with

Code:
SELECT * FROM (
	SELECT DISTINCT col1 FROM t1 WHERE...
	UNION ALL
	SELECT DISTINCT col1 FROM t1 WHERE...
) AS tbl
GROUP BY tbl.col1 HAVING COUNT(*) = 2
it worked well.
DO you mean INNER JOIN can do the same thing too?
Reply With Quote
  #6 (permalink)  
Old 11-10-08, 00:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
no, INNER JOIN was for your second problem, the one with the two tables that had only a single column in each of them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 11-10-08, 00:20
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Quote:
Originally Posted by r937
no, INNER JOIN was for your second problem, the one with the two tables that had only a single column in each of them

strange, I used the UNION ALL method for the case of two tables that had only a single column in each of them. It worked well. Why?
Reply With Quote
  #8 (permalink)  
Old 11-10-08, 06:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it worked well because it works

INNER JOIN is simpler and more efficient
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 11-10-08, 10:18
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Thanks, r937.

Do you mean both 'INNER JOIN' and 'UNION ALL' works in the case of the two tables that had only a single column in each of them.

On the other hand, *ONLY* 'UNION ALL' works in the cases when there are more than one columns/fields in the table?

Could you please show me how to implement set intersection using 'INNER JOIN' for the case of the two tables that had only a single column in each of them
Reply With Quote
  #10 (permalink)  
Old 11-10-08, 10:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by cy163
Could you please show me how to implement set intersection using 'INNER JOIN' for the case of the two tables that had only a single column in each of them
Code:
SELECT tb1.Employee_ID
  FROM tb1
INNER
  JOIN tb2
    ON tb2.Employee_ID = tb1.Employee_ID
__________________
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