SELECT mem_id FROM members WHERE mem_id NOT IN (SELECT mem_id FROM position_1) would give you the men_id's in members that aren't in position_1.
You can extend the NOT IN (SELECT... to include the other position tables using a standard JOIN.
The result (untested) should be those mem_id's that only feature in the members table. I hope its right anyway;)
hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
A cleaner table structure would have made this easier.
I'd propose to have a separate position table as a n:1 child table of members which records the positions:
create table positions (mem_id integer not null, position enum(1,2,3,4,5,6,7), foreign key (mem_id) references members (mem_id));
Then you could state your query as:
select mem_id from members where not (mem_id in( select (mem_id from positions));
or
select m.mem_id from members m left join positions p on m.mem_id=p.mem_id where p.mem_id is null;
smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
In the long run you will change the table structure. If you're now starting to get in trouble because of performance issues, chances are that you will find a workaround - which will work until the next magnitude of records has been reached.
As a workaround you might try to split the UNION subquery into single conditions. I'm not sure how the query optimizer will handle it, but it might be worth a try:
SELECT count(mem_id)
FROM members WHERE mem_status = 'F'
AND (mem_id NOT IN (SELECT mem_id FROM position_1))
AND (mem_id NOT IN (SELECT mem_id FROM position_2))
AND (mem_id NOT IN (SELECT mem_id FROM position_3))
AND (mem_id NOT IN (SELECT mem_id FROM position_4))
AND (mem_id NOT IN (SELECT mem_id FROM position_5))
AND (mem_id NOT IN (SELECT mem_id FROM position_6))
AND (mem_id NOT IN (SELECT mem_id FROM position_7))
or
SELECT count(mem_id)
FROM members WHERE mem_status = 'F'
AND NOT (
mem_id IN (SELECT mem_id FROM position_1)
OR mem_id IN (SELECT mem_id FROM position_2)
OR mem_id IN (SELECT mem_id FROM position_3)
OR mem_id IN (SELECT mem_id FROM position_4)
OR mem_id IN (SELECT mem_id FROM position_5)
OR mem_id IN (SELECT mem_id FROM position_6)
OR mem_id IN (SELECT mem_id FROM position_7)
)
smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254