向表中插入數據:1.判斷該條數據是否存在。2.若是存在則更新,若是不存在則插入。
mysql
在 SQL Server 中能夠這樣處理:sql
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中能夠這樣處理:
ui
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操做的數據表必須具備主鍵或惟一索引,不然會直接插入數據)。code
例:表結構以下。
索引
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條數據:rem
+----+----------+-----+------+ | 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');get
replace into uid set id=1,name='niubao',age=10,sex='m';table
replace into uid(id,name,age,sex) select 1,'niubao',10,'m';
class
這三種寫法均可以把id=1的數據中age修改成10。date
而當更新數據的時候須要根據原來的值進行計算的時候,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你們用的時候必定要注意表結構哦。。