2010-08-04 66 views
6

我可以使用mysql删除加入?

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; 

回答

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的delete文档了解更多信息。更好的方法可能是将received_txts的外键约束发送出去,然后级联删除。

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;