Heim > Datenbank > MySQL-Tutorial > Hauptteil

mysql一次更新多条不同的记录

WBOY
Freigeben: 2016-06-07 15:19:45
Original
1105 Leute haben es durchsucht

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 最近oschina上又有朋友问到了mysql中一次更新多条不同的记录的方法,我知道的方法有两种,使用on duplicate key update语法和使用 replace into语法。 这两个语法都需要主键索引或唯一索引支持,下面

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  最近oschina上又有朋友问到了mysql中一次更新多条不同的记录的方法,我知道的方法有两种,使用on duplicate key update语法和使用 replace into语法。

  这两个语法都需要主键索引或唯一索引支持,下面举例说明。

  测试用的表结构和数据

  1

  CREATE TABLE `t` (

  2

  `id` int(11) NOT NULL AUTO_INCREMENT,

  3

  `c1` varchar(50) NOT NULL DEFAULT '',

  4

  `c2` varchar(50) NOT NULL DEFAULT '',

  5

  `c3` varchar(50) NOT NULL DEFAULT '',

  6

  PRIMARY KEY (`id`),

  7

  UNIQUE KEY `c1` (`c1`)

  8

  ) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8 ;

  9

  insert into t values(1,2,3,4),(5,6,7,8);

  on duplicate key update 语法

  If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row.

  它会先执行插入操作,碰到有主键或唯一索引的列发生冲突时,对冲突的这一行,执行update操作,更新sql语句中指定的某几列。如果所有的列都不冲突,此语法和简单的insert into语法效果一样。例如:

  01

  mysql> insert into t (id,c1,c2)values(1,20,30),(5,60,70) on duplicate key update c1=values(c1),c2=values(c2);

  02

  Query OK, 4 rows affected (0.00 sec)

  03

  Records: 2  Duplicates: 2  Warnings: 0

  04

  05

  mysql> select * from t;

  06

  +----+----+----+----+

  07

  | id | c1 | c2 | c3 |

  08

  +----+----+----+----+

  09

  |  1 | 20 | 30 | 4  |

  10

  |  5 | 60 | 70 | 8  |

  11

  +----+----+----+----+

  12

  2 rows in set (0.00 sec)

  结果是c1,c2这两列被更新了,c3这一列没有变。

  replace into 语法

  replace into

  REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

  replace和insert所作的工作完全相同,区别是当碰到有主键或唯一索引的列发生冲突时,对冲突的这一行,在insert前会先对这行数据执行delete操作。效果是这一行中没有被指定值的列会被更新成本列的默认值,如果所有的列都不冲突,此语法和简单的insert into语法效果一样。例如:

  完全替换两行记录

  01

  mysql> replace into t (id,c1) values(1,200),(5,600);

  02

  Query OK, 4 rows affected (0.00 sec)

  03

  Records: 2  Duplicates: 2  Warnings: 0

  04

  05

  mysql> select * from t;

  06

  +----+-----+----+----+

  07

  | id | c1  | c2 | c3 |

  08

  +----+-----+----+----+

  09

  |  1 | 200 |    |    |

[1] [2] 

mysql一次更新多条不同的记录

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!