How to replace NOT EXISTS nested select with JOIN
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 `me开发者_开发百科mbers`.`number` = `cr`.`alt_reg_no`
)
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
精彩评论