简体   繁体   English

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

[英]PHP Delete rows from MySQL where two tables don't match

I have two tables: 我有两个表:

tickets
tickets_updates

I think there are a few extra rows in tickets_updates where a record doesn't exist in the tickets table 我认为tickets_updates中还有一些额外的行,而tickets表中不存在记录

What is the best way to run SQL Code to say: 运行SQL代码的最佳方式是:

Delete all from tickets_updates where ticket_seq is not in tickets (sequence) tickets_updates中删除ticket_seq不在tickets所有tickets (顺序)

sequence in the tickets table matches ticket_seq in the tickets_updates table 票证表中的序列与tickets_updates表中的ticket_seq匹配

DELETE FROM tickets_updates
WHERE  ticket_seq NOT IN (SELECT sequence
                           FROM   tickets)  
DELETE FROM tickets_updates tu
LEFT JOIN tickets t ON tu.ticket_seq = t.sequence
WHERE t.sequence IS NULL

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM