向表中插入数据:1.判断该条数据是否存在。2.如果存在则更新,如果不存在则插入。
在 SQL Server 中可以这样处理:
if not exists (select 1 from t where id = 1)
insert into t(id, update_time) values(1, getdate())
else
update t set update_time = getdate() where id = 1
在Mysql中可以这样处理:
1. replace into tbl_name(col_name, ...) values(...)
2. replace into tbl_name(col_name, ...) select ...
3. replace into tbl_name set col_name=value, ...
replace into和insert into功能类似,区别是当使用replace into操作已存在的数据时(根据主键和唯一索引判断),会先删掉原有数据,再插入一条(注意:使用replace into操作的数据表必须具有主键或唯一索引,否则会直接插入数据)。
例:表结构如下。
mysql> show create table uid;
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+
| Table | Create Table
|
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+
| uid | CREATE TABLE `uid` (
`id` int(3) NOT NULL auto_increment,
`name` char(10) NOT NULL,
`age` int(2) NOT NULL,
`sex` enum('m','w') default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+
表中有3条数据:
+----+----------+-----+------+
| id | name | age | sex |
+----+----------+-----+------+
| 1 | niubao | 30 | m |
| 2 | pidaihou | 27 | w |
| 3 | pidaihou | 1 | w |
+----+----------+-----+------+
replace into uid(id,name,age,sex) values(1,'niubao',10,'m');
replace into uid set id=1,name='niubao',age=10,sex='m';
replace into uid(id,name,age,sex) select 1,'niubao',10,'m';
这三种写法都可以把id=1的数据中age修改为10。
而当更新数据的时候需要根据原来的值进行计算的时候,replace into是不能满足要求的,这时候可以使用insert into的高级应用:
insert into uid set id=1,name='niubao',age=20,sex='m' on duplicate key update age=age+10;
这样执行的时候发现id=1的记录存在,则会执行age=age+10操作。
以上所有情况都是建立在表中有主键id或唯一索引时,当表中即存在主键id,又存在唯一索引的时候,乐子就大了。
例:表结构如下(name字段增加了唯一索引)
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------+
| uid | CREATE TABLE `uid` (
`id` int(3) NOT NULL auto_increment,
`name` char(10) NOT NULL,
`age` int(2) NOT NULL,
`sex` enum('m','w') default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------+
+----+----------+-----+------+
| id | name | age | sex |
+----+----------+-----+------+
| 1 | niubao | 80 | m |
| 2 | pidaihou | 27 | w |
| 3 | dog | 1 | w |
+----+----------+-----+------+
可以看到,受影响记录条数为3,原表中id为1和2的两条数据被删掉了,再插入了一条id=1的新数据,so大家用的时候一定要注意表结构哦。。