该查询处理和运行,但它完全忽略了没有在第的MySQL查询忽略NOT IN功能
SELECT * FROM `offers` as `o` WHERE `o`.country_iso = '$country_iso' AND `o`.`id`
not in (select distinct(offer_id) from aff_disabled_offers
where offer_id = 'o.id' and user_id = '1') ORDER by rand() LIMIT 7
该查询处理和运行,但它完全忽略了没有在第的MySQL查询忽略NOT IN功能
SELECT * FROM `offers` as `o` WHERE `o`.country_iso = '$country_iso' AND `o`.`id`
not in (select distinct(offer_id) from aff_disabled_offers
where offer_id = 'o.id' and user_id = '1') ORDER by rand() LIMIT 7
也许你的“不”查询返回任何内容。
不应该
where offer_id='o.id'
要
where offer_id=o.id
?
非常感谢您指出了这一点,它的工作完美! –
guido有答案...它看起来像你的意思是创建一个相关的子查询。 'o.id'
被视为文字。
一些注意事项:
在你需要某种形式的担保,在NOT IN
谓词的子查询不返回NULL值。如果您没有从数据库执行该保证,则在子查询中添加WHERE/HAVING return_expr IS NOT NULL
就足以为您提供保证。
相关的子查询会在大集合上吃你的午餐,表现明智。就像那个ORDER BY rand()一样。
一般而言,反连接模式被证明是更有效的大集:
SELECT o.*
FROM offers o
LEFT
JOIN aff_disabled_offers d
ON d.user_id = '1'
AND d.offer_id = o.id
WHERE d.offer_id IS NULL
AND o.country_iso = '$country_iso'
ORDER BY rand()
LIMIT 7
在嵌套查询“o.id”也许是错误的 –