• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[MySQL] Selecting from two tables, but no duplicates

vern

Dominus
Political User
#1
I want to select from two tables. Each table has a column called "course_id". I want to select from one table if the "course_id" doesn't exist in the other table. How would I do this in one or multiple queries? I was looking at unions, joins, subqueries but I don't know if that is the most efficient or easiest solution. I've messed with all three but with no luck. Any help is appreciated. I'm only really selecting from the first table, but making sure that the "course_id" doesn't exist in the second table.
 

Khayman

I'm sorry Hal...
Political User
#2
One way would be somthing like

SELECT * FROM table1 t1 WHERE t1.course_id NOT IN (select t2.course_id from table2 t2)

Also might want to look up the EXISTS command
 

vern

Dominus
Political User
#4
Kayman, thanks, that worked out really well. I was playing with EXISTS and NOT EXISTS before, but it seems NOT IN gave me the desired results.

I also played around with joins but with no luck. Thanks.
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies