6

我可以用

select * from sent_txts s 
LEFT JOIN received_txts r ON s.msg_link_id = r.id 
WHERE r.action_id = 6;

顶部选择匹配的行,

如何编写查询以删除双方的匹配行?

就像是

delete sent_txts s 
LEFT JOIN received_txts r ON s.msg_link_id = r.id 
WHERE r.action_id = 6;
4

2 回答 2

5

免责声明:我目前无法访问 mysql 数据库进行测试,但我认为您可以使用:

delete s, r from send_txts s left join received_txts r on s.msg_link_id = r.id where r.action_id = 6;

有关详细信息,请参阅 mysql 的删除文档。更好的方法可能是从 received_txts 中放置一个外键约束来发送,然后级联删除。

于 2010-08-04T03:43:46.190 回答
3

我个人更喜欢 USING 子句,但前面的答案同样有效。我支持研究使用外键约束的建议,这是一种更有效的方式来实现这一点。

DELETE FROM s, r USING sent_txts s LEFT JOIN received_txts r ON s.msg_link_id = r.id WHERE r.action_id = 6;
于 2010-08-04T03:55:56.350 回答