这条语句的规则如下:
如果你插入的记录导致一个UNIQUE索引或者primary key(主键)出现重复,那么就会认为该条记录存在,则仅执行update语句,反之,执行insert语句。 on duplicate key update
与replace into
的不同点在于:
-
当传入字段包含了唯一索引或者主键id
,replace into
是先删除在插入,create_time
与update_time
会更新成当前时间
-
当传入字段包含了唯一索引或者主键id
,on duplicate key update
是直接更新,create_time
不变,update_time
会更新成当前时间
-
当 传入字段VALUES(value1, value2, value3, ...)
中对应字段的值和数据库一摸一样,就不会触发更新和插入,影响行数为 0 , create_time
和 update_time
都不会发生变更!

语法如下:
INSERT INTO
tablename(field1,field2, field3, ...)
VALUES(value1, value2, value3, ...)
ON DUPLICATE KEY UPDATE
field1=values(field1),field2=values(field2), field3=values(field3), ...;
真实使用案例
<insert id="replaceActualIncome" parameterType="java.util.List">
insert into platform_actual_income (
platform_id,
shop_id,
tid,
consign_time,
escrow_release_time,
kj_type,
shop_name,
country_code,
trade_status,
actual_amount,
need_check,
currency_code)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.platformId},
#{item.shopId},
#{item.tid},
#{item.consignTime},
#{item.escrowReleaseTime},
#{item.kjType},
#{item.shopName},
#{item.countryCode},
#{item.tradeStatus},
#{item.actualAmount},
#{item.currencyCode})
</foreach>
ON DUPLICATE key update
platform_id = values(platform_id),
shop_id = values(shop_id),
tid = values(tid),
consign_time = values(consign_time),
escrow_release_time = values(escrow_release_time),
kj_type = values(kj_type),
shop_name = values(shop_name),
country_code = values(country_code),
trade_status = values(trade_status),
actual_amount = values(actual_amount),
need_check = values(need_check),
currency_code = values(currency_code)
</insert>
文章目录mysql多表之间的常用操作1. 根据主键,把一个表的字段赋值到另一张表2. replace into:把一张表的数据新增或更新到另一张表mysql多表之间的常用操作 1. 根据主键,把一个表的字段赋值到另一张表user表userIdusernamepasswordsexaddrphone1张珊123男北京市15623565862李思456女北京市1562354256score表scoreIduserId
需求:把
一个表某个
字段内容复制到另
一张表的某个
字段。
实现
sql语句1: 代码如下:UPDATE file_manager_folder f1LEFT OUTER JOIN file_manager_folder f2 ON f1.name = f2.name AND f2.parent
Id = 54SET f1.parent
Id = 54 WHERE f2.name IS NULL AND f1.
id IN (1,2,3);
实现
sql语句2: 代码如下:update B set extra = A.extra from A join B on (A.
id = B.
id);
操作环境:有表game_list,字段:uid,score1,score2,seat_id,last_update;
传入参数为i_player_detail ,传入的值为多个用户的id、之前分数、之后分数、座位号,每个用户的数据用分号(;)隔开;
操作目的:将各个用户对应的属性插入到目标表对应的字段中,last_update为数据更新日期;
传入参数i_player_detail ,里面存放多个用户的信息,每个用户的一组数据用分号隔开,每个用户的信息多个,比如
“用户id,score,desk,seat;
用户id,score,desk,seat;……”
-- 使用存储过程
delimit
2.将同
一个表中两个类型一样的
字段的值互换
UPDATE t_user u1, t_user u2
SET u1.signed_time = u2.create_time,
u2.create_time = u1.signed_time
3.不同
表一列复制到另
一个列
update table1,table2 set ta
update account set business_time=created_at;
# 类型一样的两个
字段值互换
update account a1,account a2 set a1.business_time=a2.created_at,
a2.created_at=a1.business_time
业务逻辑:
由于user表中的nickname字段为身份证号码,现在需要将user表中nickname字段同步到user_profile表中的idcard字段上
UPDATE user_profile
set user_profile.idcard=(
SELECT user.nickname FROM `user` WHERE user.id=user_p...
的字段,该怎么做?
您可以使用 MySQL 的触发器来实现这个功能。具体来说,您可以创建一个触发器,当源表中的字段被更新时,触发器会自动将该字段的值插入到目标表的相应字段中。触发器的语法如下:
CREATE TRIGGER trigger_name
AFTER UPDATE ON source_table
FOR EACH ROW
BEGIN
UPDATE target_table SET target_field = NEW.source_field WHERE target_id = NEW.source_id;
其中,trigger_name 是触发器的名称,source_table 是源表的名称,source_field 是源表中要更新的字段名称,target_table 是目标表的名称,target_field 是目标表中要更新的字段名称,source_id 和 target_id 是源表和目标表中用于关联的字段名称。请根据实际情况进行修改。