
WHERE NOT EXISTS
December 20, 2007In this godly world of MySQL, there’s exist this thing called WHERE NOT EXISTS who helped the stupid ‘ol me to select the things that weren’t there. Literally. This, all thanks to my guru of a colleague, Drew.
Example:
SELECT DISTINCT a.f_user_id, b.f_username, b.f_firstname,
b.f_lastname, b.f_added,
(SELECT f_raffle_number
FROM t_model_raffle
WHERE f_user_id = a.f_user_id
AND f_date >= '2007-12-10'
AND f_date <= '2007-12-21'
LIMIT 0,1)
AS f_raffle_number,
(SELECT f_date
FROM t_model_raffle
WHERE f_user_id = a.f_user_id limit 0,1)
AS f_date, b.f_birthdate, b.f_sex
FROM t_model_raffle a
INNER JOIN t_user_profile b
ON b.f_user_id = a.f_user_id
WHERE NOT EXISTS
(SELECT b.f_user_id
FROM t_buy_transaction c
WHERE a.f_user_id = c.f_user_id)