mysql插入重复的FIELD而不是KEY

发布于 2024-11-03 08:22:11 字数 1474 浏览 3 评论 0原文

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 12          |
-------------------------------------

user_id:自动递增,user_visits:默认1

INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux');

上面的语句当然会插入 3 条新记录,结果是:

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 12          |
-------------------------------------
| 3       | baz       | 1           |
-------------------------------------
| 4       | bar       | 1           |
-------------------------------------
| 5       | qux       | 1           |
-------------------------------------

但我想要实现的是:

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 13          |
-------------------------------------
| 3       | baz       | 1           |
-------------------------------------
| 4       | qux       | 1           |
-------------------------------------

从字面上看,

如果字段 user_name 存在,则更新 user_visits,否则插入一条新记录。

是否可以通过单个插入语句来实现这一点?

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 12          |
-------------------------------------

user_id: auto increament, user_visits: default 1

INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux');

the above statement will of course insert 3 new records, as the result:

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 12          |
-------------------------------------
| 3       | baz       | 1           |
-------------------------------------
| 4       | bar       | 1           |
-------------------------------------
| 5       | qux       | 1           |
-------------------------------------

but what I'm trying to achieve is:

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1       | foo       | 5           |
-------------------------------------
| 2       | bar       | 13          |
-------------------------------------
| 3       | baz       | 1           |
-------------------------------------
| 4       | qux       | 1           |
-------------------------------------

so literally,

if field user_name exists, update user_visits, else insert a new record.

is it possible to achieve this with a single insert statement?

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

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

发布评论

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

评论(3

几度春秋 2024-11-10 08:22:11

当然有,但它与您的 insert 语句无关。您需要在 user_name 列上添加唯一索引:

create unique index user_name_idx on yourtable (user_name);

然后在跟踪计数的代码中必须决定是进行插入还是更新。

Sure there is but it has nothing to do with your insert statement. You need to add a unique index on the user_name column:

create unique index user_name_idx on yourtable (user_name);

Then afterward in your code that tracks the count will have to decide whether to do an insert or an update.

ˉ厌 2024-11-10 08:22:11

您必须为用户名字段创建一个键,然后使用 INSERT ON DUPLICATE 查询来更新列值。

例如,您的查询必须是,

   INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux') 
   ON DUPLICATE KEY UPDATE user_visits=user_visits+1;

如需进一步参考,请访问 http: //dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

You have to create a key for your username field and then use INSERT ON DUPLICATE query to update the columns values.

For example your query must be,

   INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux') 
   ON DUPLICATE KEY UPDATE user_visits=user_visits+1;

For further reference visit http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

热风软妹 2024-11-10 08:22:11

您可以按原样保留 INSERT 语句并实现一个处理特殊操作的触发器。

请参阅:触发器简介

You could leave your INSERT statement as is and implement a trigger that handles your special actions.

See: An Introduction To Triggers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文