Basically, my 0 OR 0 is returning 1 – that’s not really what I wanted 🙂 I’m not really sure why because if I select 0 OR 0 I get 0..
I think this could be because is_premium_dealer is an enum(‘0′,’1’) rather than a bool (don’t ask me why, I didn’t design this database!)
Does anyone know if this is because of the ENUM, and if so how I’d cast it to an integer?
Thanks in advance!
John.
SELECT is_premium_dealer, company_name,
(SELECT COUNT(*) FROM adverts WHERE user_id = users.id AND is_archived = 0) AS innerquery,
((SELECT COUNT(*) FROM adverts WHERE user_id = users.id AND is_archived = 0 AND is_deleted = 0) > 0 OR is_premium_dealer = 1) AS logicissue
FROM `user_profiles_dealers`
LEFT JOIN `users` ON user_profiles_dealers.user_id = users.id
LEFT JOIN `counties` ON user_profiles_dealers.county_id = counties.id
LEFT JOIN `countries` ON user_profiles_dealers.country_id = countries.id
WHERE (users.is_active=1)
AND (((SELECT COUNT(*) FROM adverts WHERE user_id = users.id AND is_archived = 0 AND is_deleted = 0) > 0) OR is_premium_dealer > 0)
AND company_name like '%autocraft%' limit 5\G
The result:
*************************** 1. row ***************************
is_premium_dealer: 0
company_name: A Company
innerquery: 0
logicissue: 1
1 row in set (0.00 sec)
The answer was to wrap is_premium_dealer in the BINARY() function which seems to cast a string or anything into a binary value which can be evaluated as expected.