简体   繁体   English

mysql DELETE与另一个表的字段检查

[英]mysql DELETE with field check from another table

Good morning all, happy thursday morning. 大家早上好,周四早上很开心。 I wish I could have done this by myself but since I'm not a master in MySQL statements (yet) and I got lost in this DELETE query, here it goes... 我希望自己可以做到这一点但是因为我不是MySQL语句中的主人(但是)我在这个DELETE查询中迷失了,这里有...

I have to do a simple DELETE query like this, (deleting a comment by its id) 我必须这样做一个简单的DELETE查询,(通过其id删除注释)

DELETE FROM mya_news_comments WHERE comment_id='".$_GET['comment_id']."'";

but at the same time, to prevent people deleting comments throughout the website i need to ensure that the person deleting this comment is who it is supposed to be (in our case, an artist). 但与此同时,为了防止人们在整个网站上删除评论,我需要确保删除此评论的人是应该是谁(在我们的例子中是艺术家)。

I have another table mya_news which has among the fields news_id , artist_id In mya_news_comments I also have a field called news_id 我有另一个表mya_news ,其中包含字段news_idartist_idmya_news_comments我还有一个名为news_id的字段

So I need to check that I delete the comment_id of the particular artist, not of other artist. 所以我需要检查是否删除了特定艺术家的comment_id,而不是其他艺术家。 Basically i need to cross-check if the news_id field from mya_news_comments checks out with a field with same news_id from mya_news , and artist_id from mya_news is equal to $_id (which holds my artist_id ) 基本上我需要交叉检查news_id从外地mya_news_comments检查出与同场news_idmya_news ,并artist_idmya_news等于$_id (持有我artist_id

I'm really stuck here. 我真的被困在这里了。 I'd be glad to give more details if needed. 如果需要,我很乐意提供更多细节。

Thanks. 谢谢。

DELETE mya_news_comments
FROM   mya_news_comments
WHERE  mya_news_comments.comment_id = (SELECT [another_table].comment_id WHERE [cond])
   AND mya_news.artist_id = (SELECT [another_table].artist_id WHERE [cond]);

You can join tables in DELETE statements just like you can in SELECT statements: 您可以像在SELECT语句中一样在DELETE语句中连接表:

DELETE mya_news_comments
FROM   mya_news_comments JOIN mya_news USING (news_id)
WHERE  mya_news_comments.comment_id = ?
   AND mya_news.artist_id = ?

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

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