更新 MySQL 中的标签映射表

发布于 2025-01-07 23:42:54 字数 460 浏览 0 评论 0原文

我有一个 MySQL 标签映射表,其结构来自以下帖子 - 推荐用于标签或标记的 SQL 数据库设计

我有两列 - TagID 和 ItemID。我想更新某些项目(更改其标签)。例如,我有 ID=1 的项目,tagID 为 1,2,3。但在更新时,用户只需要 ID 为 3、4、5 的标签。显然我必须删除“1”和“2”,保留“3”并添加“4”和“5”。

最简单/最优雅的方法是什么(使用 INSERT INGORE'S、ON DUPLICATE UPDATE 或 MySQL 的其他“功能”)?

删除所有标签并将所有“新”标签重新插入,还是遍历所有标签并单独删除/插入?最少 1 个,最多 5 个标签。或者对于这么少量的标签/操作来说真的无关紧要?

I have a MySQL tag-mapping table with structure from the following post - Recommended SQL database design for tags or tagging

I have two columns - TagID and ItemID. I want to make an update on some item (change it's tags). For example I had item with ID=1 with tagID's 1,2,3. But on update user want's only tags with ID's 3,4,5. Obviously I have to remove the "1" and "2", keep the "3" and add "4" and "5".

What's the simplest way/most elegant to do it (use INSERT INGORE'S, ON DUPLICATE UPDATE or some other "features" of MySQL)?

Delete all and insert all "new" tags back or go through them all and delete/insert separately? It will be min 1, max 5 tags. Or it really doesn't matter for such a small number of tags/operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2025-01-14 23:42:54

如果你肯定最多有五个标签,那么将它们作为 tag1、tag2、tag3、tag4、tag5 包含在表中肯定是“最简单的”。为了更加简单,您可以只更新所有 5 个值,因此标签 3、4、5 将保存到 tag1、tag2、tag3,而 tag4 和 tag5 则保存为 NULL。显然它是不可扩展的——你肯定不想用大量(或可能无限的)标签来做到这一点。

接下来最简单的方法是删除所有内容并重新插入,但这并不是特别优雅。

我能想到的最优雅的方法是运行两个查询。第一个将删除所有不属于您的新标签集的内容。我扩展了您的示例以显示添加两个替换标签:(3,4,5,6,7)
接下来将插入所有新标签,但会根据 TagID 和 ItemID 的唯一组合索引忽略重复项。

#This creates the table and UNIQUE index, so you can understand how the next queries work
CREATE TABLE `taglinks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ItemID` int(11) NOT NULL,
  `TagID` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Linkindex` (`ItemID`,`TagID`)
);

#This inserts example data
INSERT INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,1),(1,2),(1,3),(1,4),(1,5);

#This removes tags 1 and 2
DELETE FROM `taglinks` WHERE `ItemID` = 1 AND `TagID` NOT IN (3,4,5,6,7);

#This adds two new tags (6 and 7). Tags 3, 4 and 5 are unaffected
INSERT IGNORE INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,6),(1,7),(1,3),(1,4),(1,5);

If you definitely have a maximum of five tags, including them in the table as tag1, tag2, tag3, tag4, tag5 will definitely be "simplest". For extra simplicity, you could just update all 5 values, so tags 3,4,5 are saved to tag1,tag2,tag3, with tag4 and tag5 saved as NULL. Obviously it's not scalable - you definitely wouldn't want to do that with a large number (or potentially unlimited) tags.

The next simplest would be to delete all and reinsert, but it's not particularly elegant.

The most elegant way I can think of is to run two queries. The first will remove everything that is not your new set of tags. I have extended your example to show adding two replacement tags: (3,4,5,6,7).
The next will insert all of your new tags, but will ignore duplicates, based on a UNIQUE combined index of TagID and ItemID.

#This creates the table and UNIQUE index, so you can understand how the next queries work
CREATE TABLE `taglinks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ItemID` int(11) NOT NULL,
  `TagID` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Linkindex` (`ItemID`,`TagID`)
);

#This inserts example data
INSERT INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,1),(1,2),(1,3),(1,4),(1,5);

#This removes tags 1 and 2
DELETE FROM `taglinks` WHERE `ItemID` = 1 AND `TagID` NOT IN (3,4,5,6,7);

#This adds two new tags (6 and 7). Tags 3, 4 and 5 are unaffected
INSERT IGNORE INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,6),(1,7),(1,3),(1,4),(1,5);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文