当重复项是不同的唯一列时,如何防止 MySQL 在使用 ON DUPLICATE KEY UPDATE 时自动递增主键?

发布于 2024-09-13 12:02:14 字数 843 浏览 3 评论 0原文

考虑下表:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| vendor_id   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vendor_name | varchar(100)     | NO   | UNI | NULL    |                |
| count       | int(10) unsigned | NO   |     | 1       |                |
+-------------+------------------+------+-----+---------+----------------+

我有以下 MySQL 查询:

INSERT INTO `table` 
   (`vendor_name`) 
VALUES 
   ('foobar') ON DUPLICATE KEY UPDATE `count` = `count` + 1

此查询的目的是将新的供应商名称插入表中,如果供应商名称已存在,则列计数应增加 1。当前列的 key 也会自动递增。在这些情况下如何防止MySQL自动递增主键?有没有一种方法可以通过一个查询来完成此操作?

谢谢。

Consider the following table:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| vendor_id   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vendor_name | varchar(100)     | NO   | UNI | NULL    |                |
| count       | int(10) unsigned | NO   |     | 1       |                |
+-------------+------------------+------+-----+---------+----------------+

I have the following MySQL query:

INSERT INTO `table` 
   (`vendor_name`) 
VALUES 
   ('foobar') ON DUPLICATE KEY UPDATE `count` = `count` + 1

The intent of this query is to insert a new vendor name to the table and in case the vendor name already exists, the column count should be incremented by 1. This works however the primary key of the current column will also be auto-incremented. How can I prevent MySQL from auto-incrementing the primary key in these cases? Is there a way to do this with one query?

Thank you.

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

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

发布评论

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

评论(2

笨死的猪 2024-09-20 12:02:14

这是可行的,但是当前列的主键也会自动递增。在这些情况下如何防止 MySQL 自动递增主键?

通过在值已经存在时使用 UPDATE 语句:

IF EXISTS(SELECT NULL
            FROM TABLE
           WHERE vendor_name = $vendor_name) THEN

    UPDATE TABLE
       SET count = count + 1
     WHERE vendor_name = $vendor_name

ELSE

    INSERT INTO TABLE
       (vendor_name)
    VALUES
       ($vendor_name

END IF

我尝试了 ON DUPLICATE KEY UPDATE, REPLACE INTO 的替代方法:

REPLACE INTO vendors SET vendor_name = 'foobar', COUNT = COUNT + 1

它更新了计数和vendor_id,所以情况更糟

......数据不关心数字是否不连续,只关心值是唯一的。如果您可以忍受这一点,我会使用 ON DUPLICATE UPDATE 语法,尽管我承认这种行为很奇怪(考虑使用 INSERT 语句是可以理解的)。

This works however the primary key of the current column will also be auto-incremented. How can I prevent MySQL from auto-incrementing the primary key in these cases?

By using an UPDATE statement when the value already exists:

IF EXISTS(SELECT NULL
            FROM TABLE
           WHERE vendor_name = $vendor_name) THEN

    UPDATE TABLE
       SET count = count + 1
     WHERE vendor_name = $vendor_name

ELSE

    INSERT INTO TABLE
       (vendor_name)
    VALUES
       ($vendor_name

END IF

I tried the alternative to ON DUPLICATE KEY UPDATE, REPLACE INTO:

REPLACE INTO vendors SET vendor_name = 'foobar', COUNT = COUNT + 1

It updates the count, and the vendor_id so it's worse...

The database & data doesn't care if the numbers aren't sequential, only that the values are unique. If you can live with that, I'd use the ON DUPLICATE UPDATE syntax though I admit the behaviour is weird (understandable considering using an INSERT statement).

小傻瓜 2024-09-20 12:02:14

我想这可能会做到。但这非常违背道教的原则——你确实违背了道教的原则。

可能有更好的解决方案。

INSERT INTO `table` 
   (`vendor_name`) 
VALUES 
   ('foobar') ON DUPLICATE KEY UPDATE `count` = `count` + 1, `vendor_id`=`vendor_id`-1

I think this might do it. But it's very much against the principles of Daoism - you're really going against the grain.

There is probably a better solution.

INSERT INTO `table` 
   (`vendor_name`) 
VALUES 
   ('foobar') ON DUPLICATE KEY UPDATE `count` = `count` + 1, `vendor_id`=`vendor_id`-1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文