Mysql – 键的列表不在subselect中

我有这样的查询:

SELECT DISTINCT(obt.oid) AS oid, o.borrower_email
FROM `order_borrower_tracker` obt 
LEFT JOIN `orders` o ON obt.oid=o.oid
WHERE obt.date_acknowledged_edelivery = 0 
AND 18 NOT IN 
    (SELECT DISTINCT(new_status) AS new_status 
    FROM `order_status_history` 
    WHERE oid=obt.oid)

哪个有效,但现在我需要更新这个查询,这样不仅“18”不在subselect中,而且还有其他几个数字….类似于

SELECT DISTINCT(obt.oid) AS oid, o.borrower_email
FROM `order_borrower_tracker` obt 
LEFT JOIN `orders` o ON obt.oid=o.oid
WHERE obt.date_acknowledged_edelivery = 0 
AND (18,27,29,41,53) NOT IN 
    (SELECT DISTINCT(new_status) AS new_status 
    FROM `order_status_history` 
    WHERE oid=obt.oid)

但显然“(18,27,29,41,53)NOT IN”不起作用.

实现这一目标的好方法是什么?不为每个数字键入AND

最佳答案 使用LEFT JOIN和IS NULL的技巧来查找没有列出状态的任何记录的订单:

SELECT DISTINCT(obt.oid) AS oid, o.borrower_email
FROM `order_borrower_tracker` obt 
LEFT JOIN `orders` o 
    ON obt.oid=o.oid
LEFT JOIN `order_status_history` h
    ON h.oid=obt.oid AND h.new_status IN (18,27,29,41,53)
WHERE obt.date_acknowledged_edelivery = 0 
AND h.oid IS NULL
点赞