MySQL INSERT ... ON DUPLICATE KEY UPDATE

╰+哭是因爲堅強的太久メ 2022-10-15 13:57 252阅读 0赞

备注:测试数据库版本为MySQL 8.0

真实环境中会有一种应用场景,如存在更新,不存在则更新,Oracle中有merge语句,可以实现此功能。
MySQL中可以用INSERT … ON DUPLICATE KEY UPDATE来实现。

文章目录

  • 一.从一个简单的例子看INSERT … ON DUPLICATE KEY UPDATE
  • 二.模拟一个同事存在insert+update的例子

一.从一个简单的例子看INSERT … ON DUPLICATE KEY UPDATE

代码:

  1. create table t1(a int, b int not null, c int not null ,primary key (a));
  2. INSERT INTO t1 (a,b,c) VALUES (1,2,3);

如果指定了ON DUPLICATE KEY UPDATE子句,而要插入的行将导致UNIQUE索引或PRIMARY KEY中出现重复值,则对旧行进行更新.

  1. INSERT INTO t1 (a,b,c) VALUES (1,2,3)
  2. ON DUPLICATE KEY UPDATE c=c+1;
  3. UPDATE t1 SET c=c+1 WHERE a=1;

测试记录:

  1. mysql> create table t1(a int, b int not null, c int not null ,primary key (a));
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> INSERT INTO t1 (a,b,c) VALUES (1,2,3);
  4. Query OK, 1 row affected (0.00 sec)
  5. mysql> select * from t1;
  6. +---+---+---+
  7. | a | b | c |
  8. +---+---+---+
  9. | 1 | 2 | 3 |
  10. +---+---+---+
  11. 1 row in set (0.00 sec)
  12. mysql> INSERT INTO t1 (a,b,c) VALUES (1,2,3)
  13. -> ON DUPLICATE KEY UPDATE c=c+1;
  14. Query OK, 2 rows affected (0.00 sec)
  15. mysql> select * from t1;
  16. +---+---+---+
  17. | a | b | c |
  18. +---+---+---+
  19. | 1 | 2 | 4 |
  20. +---+---+---+
  21. 1 row in set (0.00 sec)
  22. mysql> UPDATE t1 SET c=c+1 WHERE a=1;
  23. Query OK, 1 row affected (0.01 sec)
  24. Rows matched: 1 Changed: 1 Warnings: 0
  25. mysql> select * from t1;
  26. +---+---+---+
  27. | a | b | c |
  28. +---+---+---+
  29. | 1 | 2 | 5 |
  30. +---+---+---+
  31. 1 row in set (0.00 sec)
  32. mysql>

二.模拟一个同事存在insert+update的例子

数据接着上例数据,模拟一个insert+update的例子。

如果不存在则insert,如果存在 c = a + b

代码:

  1. select * from t1;
  2. INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4, 5, 6)
  3. ON DUPLICATE KEY UPDATE c=a+b;
  4. select * from t1;

测试记录:

  1. mysql> select * from t1;
  2. +---+---+---+
  3. | a | b | c |
  4. +---+---+---+
  5. | 1 | 2 | 5 |
  6. +---+---+---+
  7. 1 row in set (0.00 sec)
  8. mysql>
  9. mysql>
  10. mysql>
  11. mysql>
  12. mysql> INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4, 5, 6)
  13. -> ON DUPLICATE KEY UPDATE c=a+b;
  14. Query OK, 3 rows affected (0.00 sec)
  15. Records: 2 Duplicates: 1 Warnings: 0
  16. mysql> select * from t1;
  17. +---+---+---+
  18. | a | b | c |
  19. +---+---+---+
  20. | 1 | 2 | 3 |
  21. | 4 | 5 | 6 |
  22. +---+---+---+
  23. 2 rows in set (0.00 sec)
  24. mysql>

在测试一个insert into select
代码:

  1. select * from t1;
  2. INSERT INTO t1 (a,b,c)
  3. select tmp.a,tmp.b,tmp.c from
  4. (
  5. select a, b, c from t1
  6. union all
  7. select 100, 101, 102
  8. ) tmp
  9. ON DUPLICATE KEY UPDATE c=tmp.a + tmp.b;
  10. select * from t1;

测试记录:

  1. mysql> select * from t1;
  2. +---+---+---+
  3. | a | b | c |
  4. +---+---+---+
  5. | 1 | 2 | 3 |
  6. | 4 | 5 | 6 |
  7. +---+---+---+
  8. 2 rows in set (0.00 sec)
  9. mysql> INSERT INTO t1 (a,b,c)
  10. -> select tmp.a,tmp.b,tmp.c from
  11. -> (
  12. -> select a, b, c from t1
  13. -> union all
  14. -> select 100, 101, 102
  15. -> ) tmp
  16. -> ON DUPLICATE KEY UPDATE c=tmp.a + tmp.b;
  17. Query OK, 3 rows affected (0.01 sec)
  18. Records: 3 Duplicates: 1 Warnings: 0
  19. mysql> select * from t1;
  20. +-----+-----+-----+
  21. | a | b | c |
  22. +-----+-----+-----+
  23. | 1 | 2 | 3 |
  24. | 4 | 5 | 9 |
  25. | 100 | 101 | 102 |
  26. +-----+-----+-----+
  27. 3 rows in set (0.00 sec)
  28. mysql>

参考:
1.https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读