MySQL query, select what hasn't been selected.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bobthebob1234
Forum Newbie
Posts: 2
Joined: Fri Aug 28, 2009 10:04 am

MySQL query, select what hasn't been selected.

Post 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
...
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post by Darhazer »

you need something like this...

Code: Select all

WHERE day_id NOT IN (SELECT day_id FROM duty_days)
bobthebob1234
Forum Newbie
Posts: 2
Joined: Fri Aug 28, 2009 10:04 am

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

Post by bobthebob1234 »

OMG WOW!

A little tweek and that is perfect!

Thanks.
Post Reply