简体   繁体   English

MySQL Delete Row在需要时不删除多对多表中的值

[英]MySQL Delete Row not removing values in many-to-many table when needed

I am implementing my own blogging system to better understand the object oriented side of PHP, mySQL, and jQuery AJAX. 我正在实现自己的博客系统,以更好地理解PHP,mySQL和jQuery AJAX的面向对象方面。 I have a many-to-many relationship between Post_T and Tag_T, thus including an associative entity PostTag_T. 我在Post_T和Tag_T之间有多对多关系,因此包括一个关联实体PostTag_T。 When creating a post, I added functions that would check if the tag already exists, and either add the new tag and mapping, or just add the mapping to the existing tag. 创建帖子时,我添加了一些功能来检查标记是否已存在,然后添加新标记和映射,或者仅将映射添加到现有标记。

The mishap that I noticed is that when deleting a Post, the tag row in Tag_T still exists while the mapping in PostTag_T is removed (set to ON DELETE CASCADE). 我注意到的一个不幸是,在删除帖子时,在删除PostTag_T中的映射(设置为ON DELETE CASCADE)时,Tag_T中的标签行仍然存在。 How can I go about deleting the tag, if it isn't being used by any other post? 如果没有其他帖子正在使用该标签,该如何删除? I have the post ID while deleting the Post. 删除帖子时,我具有帖子ID。

Useful Logic 有用的逻辑
Using the post id from deletion, delete from tag_t where in posttag_t the pID = post id AND no other posts (pID) in posttag_t is using the same tag id . 使用删除中的帖子ID,从tag_t中删除,其中在posttag_t中,pID =帖子ID,并且posttag_t中没有其他帖子(pID)使用相同的标签id

delete from tag_t where tID = (select tID from posttag_t where pID='post id') AND..


For your sanity, here is a visual representation 为了您的理智,这是一个视觉表示

Created Post row in Post_T: 在Post_T中创建的帖子行:

mysql创建帖子

Mapping between post and tag in PostTag_T: PostTag_T中post和tag之间的映射:

mysql关联实体

Created Tag row in Tag_T: 在Tag_T中创建的标签行:

mysql创建标签

When Deleting: using AJAX POST with data that calls a php file in url (guessing this is where I might need to add a query that searches tables..) 删除时:将AJAX POST与在url中调用php文件的数据一起使用(猜测这是我可能需要添加查询表的查询的地方。)

MySQL的PHP​​管理职位

DELETE FROM `tag_t` WHERE 
`tID` IN (SELECT `tID` FROM `posttag_t` WHERE `pID` = 'post_id') AND 
`tID` NOT IN (SELECT `tID` FROM `posttag_t` WHERE `pID` != 'post_id')

The last part of above query check is there any post using this tag or not. 以上查询检查的最后一部分是否有任何使用此标记的帖子。

When you delete a post due to ON DELETE CASCADE you no longer have rows in posttag_t table thus it's useless to try to match against it. 当您由于ON DELETE CASCADE删除帖子时, posttag_t表中不再有行,因此尝试与其匹配是没有用的。 That means that you left with only option to delete all tags that are not associated with any posts. 这意味着您只剩下删除与任何帖子无关的所有标签的选项。

You can do that with a query 您可以通过查询来做到这一点

DELETE t
  FROM tag_t t
 WHERE NOT EXISTS
(
  SELECT *
    FROM posttag_t 
   WHERE tid = t.tid
);

Here is SQLFiddle demo 这是SQLFiddle演示


Now you can let a trigger to take care of that 现在,您可以让触发器来解决这个问题

CREATE TRIGGER gt_ad_posttag_t
AFTER DELETE ON post_t
FOR EACH ROW
  DELETE t
    FROM tag_t t
   WHERE NOT EXISTS
  (
    SELECT *
      FROM posttag_t 
     WHERE tid = t.tid
  );

Here is SQLFiddle demo 这是SQLFiddle演示

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

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