PHP从MySQL中删除两个表不匹配的行

时间:2013-07-23 12:36:39

标签: php mysql

我有两张桌子:

tickets
tickets_updates

我认为tickets_updates中有一些额外的行,tickets表中不存在记录

运行SQL代码的最佳方式是:

tickets_updates ticket_seq不在tickets(序列)中删除所有内容

票证表中的

序列与tickets_updates表中的ticket_seq匹配

3 个答案:

答案 0 :(得分:5)

DELETE FROM tickets_updates
WHERE  ticket_seq NOT IN (SELECT sequence
                           FROM   tickets)  

答案 1 :(得分:3)

DELETE FROM tickets_updates tu
LEFT JOIN tickets t ON tu.ticket_seq = t.sequence
WHERE t.sequence IS NULL

答案 2 :(得分:-1)

试试这个:

DELETE FROM ticket_updates
WHERE NOT EXISTS
    (SELECT 'x' from tickets
    WHERE tickets.sequence = ticket_updates.ticket_seq)