我有一个很长的MySql查询,里面有很多过滤器,可以将我的结果缩小到我需要的一小部分答案。以下是查询的用法:
SELECT o.offer_id, o.external_cat, o.cat, o.shop, o.item_id, o.live
FROM ourson_offerattributes_new oa1
JOIN ourson_offerattributes_new oa2 ON oa1.offer_id=oa2.offer_id
AND oa1.attribute_id = 'most_recent_status'
JOIN ourson_offerattributes_new oa3 ON oa2.offer_id=oa3.offer_id
AND oa3.attribute_id = oa2.value AND oa3.value ='OK'
JOIN ourson_offer_new o ON (o.offer_id = oa3.offer_id
AND cat IN (5051511,5051512,5051513,5051514,5051507,5051508,5051509,5051510,5051505,5051506,5051500,5051501,5051502,5051503,5060402))
LEFT JOIN ourson_offerattributes_new oa4 ON oa3.offer_id=oa4.offer_id
AND oa4.attribute_id='status_ltu_indexer' AND oa4.value='OK' AND oa3.offer_id IS NULL
LEFT JOIN ourson_offer_new oa5 ON oa3.offer_id=oa5.offer_id
AND oa5.cat IS NULL AND oa5.cat IN (5070101,7030100,2020205,2020202)
当我使用它时,此查询有效,但我注意到我的LEFT JOIN
子句中的一个完全被忽略,我可以在其中添加任何过滤器,MySQL只是忽略它而我不会这样做。似乎知道为什么没有引起错误...
这是被忽略的行:
LEFT JOIN ourson_offerattributes_new oa4 ON oa3.offer_id=oa4.offer_id
AND oa4.attribute_id='status_ltu_indexer' AND oa4.value='OK' AND oa3.offer_id IS NULL
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
问题可能在于:
AND oa3.offer_id IS NULL
由于NULL
用于将JOIN
连接到结果集,因此永远不会oa3
。
更新#1:
请替换以下部分查询:
LEFT JOIN ourson_offerattributes_new oa4 ON oa3.offer_id=oa4.offer_id
AND oa4.attribute_id='status_ltu_indexer' AND oa4.value='OK' AND oa3.offer_id IS NULL
这一个:
INNER JOIN ourson_offerattributes_new oa4 ON oa3.offer_id=oa4.offer_id
AND oa4.attribute_id='status_ltu_indexer' AND oa4.value='OK'
另请考虑更换部件:
LEFT JOIN ourson_offer_new oa5 ON oa3.offer_id=oa5.offer_id
AND oa5.cat IS NULL AND oa5.cat IN (5070101,7030100,2020205,2020202)
使用:
INNER JOIN ourson_offer_new oa5 ON oa3.offer_id=oa5.offer_id
AND oa5.cat IN (5070101,7030100,2020205,2020202)
答案 1 :(得分:0)
您不能使用算术比较运算符,例如=,<或<>测试NULL由于与NULL进行任何算术比较的结果也是NULL,因此无法从此类比较中获得任何有意义的结果。显然,您需要oa3.offer_id为NULL。你在这里做比较:
oa3.offer_id=oa4.offer_id