我有两张桌子:
tickets
tickets_updates
我认为tickets_updates
中有一些额外的行,tickets
表中不存在记录
运行SQL代码的最佳方式是:
从tickets_updates
ticket_seq
不在tickets
(序列)中删除所有内容
序列与tickets_updates表中的ticket_seq匹配
答案 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)