Page 1 of 1

MySQL query, select what hasn't been selected.

Posted: Fri Aug 28, 2009 10:11 am
by bobthebob1234
I think this is the right board to post this in. Kinda new here. Anyways;

Basically i want to select everything in a table which isn't in another one...

ok, that made more sense in my head.
I have 3 tables, 'days' 'duties' and 'duty_days'
duty_days has duty_id and day_id fields (from the other two tables)
what I want to do is select all the duties which are on the same day_id, then the rest of the duties.

hope this is making sense so far...

currently I have

Code: Select all

SELECT `title`, `duties`.`duty_id` FROM `duties`, `duty_days` WHERE `duty_days`.`day_id` = "' . $d . '" AND `duty_days`.`duty_id` = `duties`.`duty_id` ORDER BY `duties`.`start_time` ASC
I then want to select all the `titles` from `duties` which weren't in the orginal query.

Any ideas how to do this?

thanks

oh yer `days` just has `name` and `day_id`, eg
monday 1
tuesday 2
wednesday 3
...

Re: MySQL query, select what hasn't been selected.

Posted: Fri Aug 28, 2009 11:14 am
by Darhazer
you need something like this...

Code: Select all

WHERE day_id NOT IN (SELECT day_id FROM duty_days)

Re: MySQL query, select what hasn't been selected.

Posted: Fri Aug 28, 2009 1:00 pm
by bobthebob1234
OMG WOW!

A little tweek and that is perfect!

Thanks.