批量插入/更新mysql

时间:2018-04-07 12:50:38

标签: mysql sql sql-update

假设我想执行以下mysql并且表中已经存在很少的记录,并且可能存在多条记录。 如果此记录(21,'Helen','a')(21,'Hui Ling','b')已经存在于表中,并且我想执行插入和更新,那么条件如.. where name = "Helen" and grade="a"那么应该是我的更新查询部分。

INSERT INTO students (points,name,grade)
    VALUES
    (21,'Helen','b'),
    (21,'Helen','c' ),
    (21,'Samia','a'),
    (21,'Hui Ling','b'),
    (21,'Yumie','a'),
    (21,'Helen','a')
ON DUPLICATE KEY UPDATE
    grade = VALUES('a'),

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望将grade设置为传入的内容。您想使用VALUES()构造:

INSERT INTO students (points, name, grade)
    VALUES
        (21,'Helen','b'),
        (21,'Helen','c' ),
        (21,'Samia','a'),
        (21,'Hui Ling','b'),
        (21,'Yumie','a'),
        (21,'Helen','a')
    ON DUPLICATE KEY UPDATE grade = VALUES(grade);

这将任意选择表中的一个值。 Here是一个SQL小提琴。

答案 1 :(得分:0)

我不清楚你想要什么,但你几乎肯定需要看看你的钥匙

MariaDB [sandbox]> drop table if exists students;
Query OK, 0 rows affected (0.17 sec)

MariaDB [sandbox]> create table students
    ->  (id int auto_increment primary key,points int,name varchar(20),grade varchar(1));
Query OK, 0 rows affected (0.19 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> INSERT INTO students (points,name,grade)
    ->     VALUES
    ->     (21,'Helen','b'),
    ->     (21,'Helen','c' ),
    ->     (21,'Samia','a'),
    ->     (21,'Hui Ling','b'),
    ->     (21,'Yumie','a'),
    ->     (21,'Helen','a'),
    ->     (22,'Helen','z')
    -> ON DUPLICATE KEY UPDATE
    ->     grade = VALUES(grade);
Query OK, 7 rows affected (0.02 sec)
Records: 7  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from students;
+----+--------+----------+-------+
| id | points | name     | grade |
+----+--------+----------+-------+
|  1 |     21 | Helen    | b     |
|  2 |     21 | Helen    | c     |
|  3 |     21 | Samia    | a     |
|  4 |     21 | Hui Ling | b     |
|  5 |     21 | Yumie    | a     |
|  6 |     21 | Helen    | a     |
|  7 |     22 | Helen    | z     |
+----+--------+----------+-------+
7 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> truncate table students;
Query OK, 0 rows affected (0.23 sec)

MariaDB [sandbox]> alter table students
    -> add unique key studentsk1(name,points);
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [sandbox]> INSERT INTO students (points,name,grade)
    ->     VALUES
    ->     (21,'Helen','b'),
    ->     (21,'Helen','c' ),
    ->     (21,'Samia','a'),
    ->     (21,'Hui Ling','b'),
    ->     (21,'Yumie','a'),
    ->     (21,'Helen','a'),
    ->     (22,'Helen','z')
    -> ON DUPLICATE KEY UPDATE
    ->     grade = VALUES(grade);
Query OK, 9 rows affected (0.02 sec)
Records: 7  Duplicates: 2  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from students;
+----+--------+----------+-------+
| id | points | name     | grade |
+----+--------+----------+-------+
|  1 |     21 | Helen    | a     |
|  2 |     21 | Samia    | a     |
|  3 |     21 | Hui Ling | b     |
|  4 |     21 | Yumie    | a     |
|  5 |     22 | Helen    | z     |
+----+--------+----------+-------+
5 rows in set (0.00 sec)

请注意,在第一个插件中,helen是重复的,因为没有用于测试的ON DUPLICATE KEY的唯一键。在alter table添加唯一键之后,以下插入检测到重复键,并按预期工作。