简体   繁体   English

如何从两个数据库表中删除数据

[英]How do I delete data using from two database table

I need to delete data from two tables at once.我需要一次从两个表中删除数据。 I have articles in one spreadsheet, and I have page permissions in the other spreadsheet that clearly specify who gets to the article and who doesn't.我在一个电子表格中有文章,而我在另一个电子表格中有页面权限,可以清楚地指定谁可以访问该文章,谁不能。 However, if I want to delete a given article, I am unable to put together the sql code to delete the permissions based on the url article.但是,如果我想删除给定的文章,我无法根据url文章将sql代码放在一起删除权限。 I tried this without any result我试过这个没有任何结果

DELETE * FROM article_permission
JOIN article ON article.article_id = article_permission.article_id
WHERE article.url = 'kjebgkwb'
DELETE ap.*, a.* 
FROM article_permission ap
JOIN article a ON a.article_id = ap.article_id
WHERE a.url = 'kjebgkwb'
DELETE p, a 
FROM article_permission p
JOIN article a ON a.article_id = p.article_id
WHERE a.url = 'kjebgkwb'

It is even easier if you set your table relation to ON DELETE CASCADE then you only need to delete from the article table and the DB does the rest for you.如果您将表关系设置为ON DELETE CASCADE容易,那么您只需要从article表中删除,数据库会为您完成其余的工作。

delete t1.*, t2.* FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.id = 'idnumber'

Or或者

You have to run two different delete query or You have to set foreign key on the other table so it will automatically delete records with this id.您必须运行两个不同的删除查询,或者您必须在另一个表上设置外键,以便它会自动删除具有此 ID 的记录。

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

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