从两个表中选择,其中第二个表中没有相关的行

时间:2014-09-01 09:31:58

标签: mysql sql

我正在执行此SQL查询:

SELECT * FROM tickets t 
WHERE t.status <> 'Completed' 
AND NOT EXISTS (SELECT u.ticketnumber 
                FROM ticket_updates u 
                WHERE u.type = 'update' AND u.ticketnumber = t.ticketnumber)

从ticket_updates表中没有行的2个表中选择两个表中的ticketnumber = ticketnumber

如何更改此查询以返回ticket_updates where customer <> 'Y'

中没有行的行

2 个答案:

答案 0 :(得分:0)

只需在子查询中添加另一个AND条件

即可
SELECT * FROM tickets t 
WHERE t.status <> 'Completed' 
AND NOT EXISTS (SELECT u.ticketnumber 
                FROM ticket_updates u 
                WHERE u.type = 'update' AND u.ticketnumber = t.ticketnumber
                AND u.customer <> 'Y'
               )

答案 1 :(得分:0)

SELECT t.*, tu.* 
FROM tickets t
LEFT JOIN ticket_updates tu ON t.ticketnumber = tu.ticketnumber 
WHERE tu.ticketnumber IS NULL AND t.status <> 'Completed' AND tu.customer <> 'Y'