语法如下-
insert into yourTableName values(yourValue1,yourValue2,..N) on duplicate key update yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N;
让我们首先创建一个表-
create table DemoTable1539 -> ( -> EmployeeName varchar(20), -> EmployeeSalary int, -> UNIQUE KEY(EmployeeName) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500; insert into DemoTable1539 values('David',5500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500; insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500;
使用select语句显示表中的所有记录-
select * from DemoTable1539;
这将产生以下输出-
+--------------+----------------+ | EmployeeName | EmployeeSalary | +--------------+----------------+ | Robert | 8500 | | David | 5500 | +--------------+----------------+ 2 rows in set (0.00 sec)