SQL "NOT IN" 函数无法正常工作
SQL "NOT IN" function not working properly
嗨,谁能找出这个 sql 查询有什么问题。
Not In 功能无法正常工作。在我的 table 中,id 1 和 2 不应该被获取,因为它们在 not in 函数中进入子查询。
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
您很可能需要用括号将 OR
条件括起来:
where
pr.cat_id in (1, 2)
and pr.id not in (...)
and ...
and (pr.placeid = 'CA' or pr.placeid = '6077243')
或者更好的是,使用 in
:
where
pr.cat_id in (1, 2)
and pr.id not in (...)
and ...
and pr.placeid in ('CA', '6077243')
请注意,我用单引号将文字数字括起来,因为 placeid
似乎是一个字符串。
我还建议将 in
条件重写为 not exists
。它通常更有效,并且 null
-safe:
where
pr.cat_id in (1, 2)
and not exists (
select 1
from ad_purchased_leads apl
where apl.user_id = 8 and apl.request_id = pr.id
)
and pr.leadsSold < pc.total_leads_allowed
and pc.active =1
and pr.status = 1
and pr.placeid in ('CA', '6077243')
为了性能,您需要 ad_purchased_leads(user_id, request_id)
上的索引。
OR
的优先级低于 AND
。您的 WHERE
转换为
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)
你可能想要
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)
嗨,谁能找出这个 sql 查询有什么问题。 Not In 功能无法正常工作。在我的 table 中,id 1 和 2 不应该被获取,因为它们在 not in 函数中进入子查询。
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
您很可能需要用括号将 OR
条件括起来:
where
pr.cat_id in (1, 2)
and pr.id not in (...)
and ...
and (pr.placeid = 'CA' or pr.placeid = '6077243')
或者更好的是,使用 in
:
where
pr.cat_id in (1, 2)
and pr.id not in (...)
and ...
and pr.placeid in ('CA', '6077243')
请注意,我用单引号将文字数字括起来,因为 placeid
似乎是一个字符串。
我还建议将 in
条件重写为 not exists
。它通常更有效,并且 null
-safe:
where
pr.cat_id in (1, 2)
and not exists (
select 1
from ad_purchased_leads apl
where apl.user_id = 8 and apl.request_id = pr.id
)
and pr.leadsSold < pc.total_leads_allowed
and pc.active =1
and pr.status = 1
and pr.placeid in ('CA', '6077243')
为了性能,您需要 ad_purchased_leads(user_id, request_id)
上的索引。
OR
的优先级低于 AND
。您的 WHERE
转换为
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)
你可能想要
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)