views:

71

answers:

1

Hey there, I'm kinda stuck trying to optimise a query which has a NOT EXISTS clause on a nested SELECT. I've been rewriting my queries containing nested selects, changing them to joins, but on this occasion I'm not sure how to combine that with the NOT EXISTS clause. I have the following query:

SELECT `reg_no`, COUNT(*) AS `records_found` FROM (`club_records` AS `cr`) WHERE NOT EXISTS ( SELECT `number` FROM `members` WHERE `members`.`number` = `cr`.`alt_reg_no` )
+1  A: 

http://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/

SELECT `reg_no`, COUNT(*) AS `records_found`,members`.`number`
FROM `club_records` AS `cr`
LEFT OUTER JOIN members`
ON  `members`.`number` = `cr`.`alt_reg_no
WHERE members`.`number` IS NULL;

attention: member number definition must include NOT NULL constraint

iddqd
Thanks! Set the member number column to NOT NULL and this works great
spinozf