Mysql - 键的列表不在subselect中

时间:2014-08-11 15:54:48

标签: mysql

我有这样的查询:

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"不是在子选择中,而是在其他几个数字......类似

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)不在"不起作用。

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

1 个答案:

答案 0 :(得分:2)

使用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