mysql一次更新多條記錄問題

2021-06-22 12:23:07 字數 3126 閱讀 1974

**:

replace into和insert into on duplicate key 區別

create table `test` (

`id` tinyint(3) unsigned not null auto_increment,

`name` char(10) not null default 」,

`dept` char(10) not null default 」,

`age` tinyint(3) unsigned not null default 『0′,

primary key (`id`),

unique key `uni_key` (`name`)

) engine=myisam auto_increment=5 default charset=latin1 |

mysql> select * from test;

| id | name    | dept       | age |

| 1 | wang   | it         | 30 |

| 2 | hong    | accounting | 20 |

| 3 | gang    | sales      | 40 |

| 4 | raymond | service    | 20 |

4 rows in set (0.00 sec)

mysql> replace into test (name,dept,age) values(『gang』,'it』,25);

query ok, 2 rows affected (0.00 sec)

mysql> select * from test;

| id | name    | dept       | age |

| 1 | wang    | it         | 30 |

| 2 | hong    | accounting | 20 |

| 5 | gang    | it         | 25 |

| 4 | raymond | service    | 20 |

4 rows in set (0.00 sec)

mysql> replace into test (name,dept,age) values(『test』,'it』,25);

query ok, 1 row affected (0.00 sec)

mysql> select * from test;

| id | name    | dept       | age |

| 1 | wang    | it         | 30 |

| 2 | hong    | accounting | 20 |

| 5 | gang    | it         | 25 |

| 4 | raymond | service    | 20 |

| 6 | test    | it         | 25 |

5 rows in set (0.00 sec)

mysql> replace into test (name,dept) values(『hong』,'sales』);

query ok, 2 rows affected (0.00 sec)

mysql> select * from test;

| id | name    | dept    | age |

| 1 | wang    | it      | 30 |

| 7 | hong    | sales   |   0 |

| 5 | gang    | it      | 25 |

| 4 | raymond | service | 20 |

| 6 | test    | it      | 25 |

5 rows in set (0.00 sec)

replace:

當沒有key衝突時,replace相當於普通的insert.

當與key衝突時,replace覆蓋相關字段,同時auto_increment累加,其它字段填充預設值,可以理解為刪除重複key的記錄,新插入一條記錄。

mysql> insert into test (name,dept,age) values(『hong』,'testing』,24)

-> on duplicate key update age=age+1;

query ok, 2 rows affected (0.00 sec)

mysql> select * from test;

| id | name    | dept    | age |

| 1 | wang    | it      | 30 |

| 7 | hong    | sales   |   1 |

| 5 | gang    | it      | 25 |

| 4 | raymond | service | 20 |

| 6 | test    | it      | 25 |

5 rows in set (0.00 sec)

mysql> insert into test (name,dept,age) values(『hong』,'manager』,24)

on duplicate key update age=100;

query ok, 2 rows affected (0.00 sec)

mysql> select * from test;

| id | name    | dept    | age |

| 1 | wang    | it      | 30 |

| 7 | hong    | sales   | 100 |

| 5 | gang    | it      | 25 |

| 4 | raymond | service | 20 |

| 6 | test    | it      | 25 |

5 rows in set (0.00 sec)

insert into …on duplicate key:

當與key衝突時,只update相應字段值。

SQL 一次插入多條記錄 例句

新增多條記錄 insert into tablename col1,col2,col3 select 1,2,3 union all select 4,5,6 union all select 7,8,9從另外的一張表中讀取多條資料新增到新錶中 insert into tablename col1,...

mysql 一次執行多條sql語句

mysqli new mysqli this db hostname,this db username,this db password,this db database 連線mysql資料庫 if mysqli connect errno mysqli multi query datastr 執行...

ms sql 一次插入多條記錄的語句

有的時候我們需要一次像資料庫中新增多條記錄,我們可以使用下面的語句來實現 新增一條記錄 insert into tablename col1,col2,col3 values 1,2,3 新增多條記錄 insert into tablename col1,col2,col3 select 3,4,5...