Old 28-01-2008, 11:09   #1 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,545
Mysql - cross table queries

I have two tables in a database:

Table 1 - files:

column 1 - ID
column 2 - deleted



Table 2 - seminar_files

column 1- seminar_id
column 2 - file_id



ID from table 1 and file_id from table 2 are the column that bridges these together. So for example, I may have a file with an ID of "haku" in table 1, and that same 'haku' may appear in table 2. It may appear zero times, one time or many times, but in such a case will have a different seminar_id for each time it appears.

I want to select all the IDs from from table 1 that aren't in table 2 when seminar_id is equal to 18 (for example). On top of this, I only want to select the files from table one where deleted is set to zero.

Anybody got an idea on how to put together this mysql query? I've tried a bunch of different ways, and I either end up with ALL the files from table one, or none!
  Reply With Quote
Old 28-01-2008, 11:20   #2 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
Are you looking at left join, right join queries. It is one of those. Last time I managed to bungle my way through by trial and error. I think that is what you need to research if that helps.
__________________
  Reply With Quote
Old 28-01-2008, 11:21   #3 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
__________________
  Reply With Quote
Old 28-01-2008, 11:40   #4 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,545
I was trying out the left join, but I just wasn't getting it to work. I went with the trial and error method for about an hour and a half before posting that! I'll try again tomorrow if no one can point me in the right way. I know the right answer is there, I just gotta figure out what it is.

Thanks though!
  Reply With Quote
Old 28-01-2008, 17:18   #5 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
SELECT f.ID FROM files f WHERE f.ID NOT IN
( SELECT file_id FROM seminar_files sf WHERE sf.seminar_id = 18 )
AND f.deleted = 0

Last edited by d3mcfadden : 28-01-2008 at 21:25.
  Reply With Quote
Old 28-01-2008, 18:12   #6 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,545
You sir are a savior! What I was trying was not anything even remotely close to that. (I didn't even know you could do that!). I'll try it out when I get to work.
  Reply With Quote
Old 28-01-2008, 21:10   #7 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,545
I just tried it out and it worked perfectly. I learned a little something from that too! Thanks!
  Reply With Quote
Old 28-01-2008, 21:27   #8 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
no problem man.. its amazing how many people don't know about the IN / NOT IN clause...
  Reply With Quote
Old 28-01-2008, 21:50   #9 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,545
I didn't, but I do now. I will definitely use that one again in the future.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8