Hi can anyone figure out what's wrong with this sql query. The Not In function is not working properly. in my table the id 1 and 2 should not fetched as they come in subquery in the not in function.
select pr.id
,pc.title as category
,pc.points_required
,pc.total_leads_allowed
,pr.leadsSold
,pr.created
from ad_pro_requests pr
join ad_pro_categories pc
on pc.id = pr.cat_id
where pr.cat_id IN(1,2)
AND pr.id NOT IN(
select request_id
from ad_purchased_leads
where user_id = 8
)
And pr.leadsSold < pc.total_leads_allowed
And pc.active =1
And pr.status = 1
And pr.placeId = 'CA'
OR pr.placeId = 6077243
ORDER by id desc
limit 0,15
You - most likely - need to surround the
OR
condition with parentheses:Or better yet, use
in
:Note that I surrounded the literal number with single quotes, since
placeid
seems to be a string.I would also suggest rewriting the
in
condition asnot exists
. It is usually more efficient, andnull
-safe:For performance, you want an index on
ad_purchased_leads(user_id, request_id)
.