删除MySQL中的相关记录 [英] Deleting related records in MySQL

查看:73
本文介绍了删除MySQL中的相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个MySQL(MyISAM)表:

I have two MySQL (MyISAM) tables:

Posts: PostID(primary key), post_text, post_date, etc. 

Comments: CommentID(primary key), comment_text, comment_date, etc.  

当从帖子"表中删除相应的帖子记录时,我想删除属于特定帖子的评论"表中的所有评论.

I want to delete all the comments in the "Comments" table belonging to a particular post, when the corresponding post record is deleted from the "Posts" table.

我知道这可以通过使用InnoDB的级联删除(通过设置外键)来实现.但是如何在MyISAM中使用PHP做到这一点?

I know this can be achieved using cascaded delete with InnoDB (by setting up foreign keys). But how would I do it in MyISAM using PHP?

推荐答案

DELETE
    Posts,
    Comments
FROM Posts
INNER JOIN Comments ON
    Posts.PostID = Comments.PostID
WHERE Posts.PostID = $post_id;

假设您的Comment表中有一个字段PostID,用于指定Comment所属的Post.

Assuming your Comments table has a field PostID, which designates the Post to which a Comment belongs to.

这篇关于删除MySQL中的相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆