添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

1. 根据主键,把一个表的字段赋值到另一张表

user

userId username password sex addr phone
1 张珊 123 北京市 1562356586
2 李思 456 北京市 1562354256

score

scoreId userId username score course phone
1 1 80 语文
2 1 85 数学

需求:根据 userId user 表的 username phone 字段填充到 score 表中

update score,user
set score.username = user.username , score.phone = user.phone
where score.userId = user.userId
update score join user on score.userId=user.userId
set score.username = user.username , score.phone = user.phone 

(生产案例)

update shopee_finances_escrow,ods_api_trade 
set shopee_finances_escrow.trade_status = ods_api_trade.trade_status
where shopee_finances_escrow.id = ods_api_trade.rec_id
and shopee_finances_escrow.shop_id = 28

2. replace into:删除或插入

把一张表的数据新增或更新到另一张表

replaceinsert的增强版

replace into 首先尝试插入数据到表中,

  1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。
  2. 否则,直接插入新数据

replace into的三种使用: 点击查看详情!

  • replace into … values …
  • replace into … select (valueA, valueB, …) from table
  • replace into tbl_name set colA=valueA, …

replace intobinlog的表现形式:注意如果有重复数据时,binlog会记录成update的形式

  • 1、replace into tbl_name(col_name, …) values(…)
    无重复数据时插入replace into t1 values(1,“liu”,28);
    在这里插入图片描述
    有重复数据时插入replace into t1 values(1,“yun”,29);
    在这里插入图片描述
    2.replace into tbl_name(col_name, …) select …
    无重复数据时插入replace into t1 select * from t2
    在这里插入图片描述
    有重复数据时插入replace into t1 select * from t2
    在这里插入图片描述

windows查看binlog步骤

  1. 查看binlog是否开启: show variables like 'log_bin';
    在这里插入图片描述

  2. 列出binlog:show binary logs; show binlog events in ‘binlog.000018’(展示细节);
    在这里插入图片描述

  3. binlog文件位于mysql的data目录下
    在这里插入图片描述

  4. binlog文件执行器位于mysql的bin目录下
    在这里插入图片描述

  5. bin目录下,执行mysqlbinlog.exe --no-defaults --base64-output=decode-rows -v ../Data/binlog.000018 > test.txt 把binlog打印到 test.txt文件中

  6. test.txt文件中查看即可
    在这里插入图片描述

replace into的应用注意事项:

  1. 插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据。
  2. replace操作在自增主键的情况下,遇到唯一键冲突时执行的是delete+insert,但是在记录binlog时,却记录成了update操作,update操作不会涉及到auto_increment的修改。备库应用了binlog之后,备库的表的auto_increment属性不变。如果主备库发生主从切换,备库变为原来的主库,写新的主库则有风险发生主键冲突 点击查看id自增时,使用replace into备机可能存在的问题!
    点击查看id自增时,使用replace into备机可能存在的问题!
<insert id="replaceIntoOrderItemData"> REPLACE INTO shopee_aggregation_order_items ( platform_id, shop_id, tid, item_id, item_name, item_sku SELECT platform_id, shop_id, tid, item_id, item_name, item_sku shopee_finances_escrow_items WHERE 1=1 <if test="shopId != null and shopId != '' "> and shop_id = #{shopId} <if test="tidList != null and tidList.size > 0"> and tid in <foreach collection="tidList" index="index" item="item" open="(" close=")" separator=","> #{item} </foreach> </insert>

3. on duplicate key update:更新或插入

这条语句的规则如下:

         如果你插入的记录导致一个UNIQUE索引或者primary key(主键)出现重复,那么就会认为该条记录存在,则仅执行update语句,反之,执行insert语句。 on duplicate key updatereplace into的不同点在于:

  • 当传入字段包含了唯一索引或者主键idreplace into 是先删除在插入,create_timeupdate_time会更新成当前时间

  • 当传入字段包含了唯一索引或者主键idon duplicate key update 是直接更新,create_time不变,update_time会更新成当前时间

  • 传入字段VALUES(value1, value2, value3, ...) 中对应字段的值和数据库一摸一样,就不会触发更新和插入,影响行数为 0 , create_timeupdate_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多表之间的常用操作&nbsp;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.parentId = 54SET f1.parentId = 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 是源和目标中用于关联的字段名称。请根据实际情况进行修改。