mysql同时修改2个表思路

1.需求:修改评论表中的昵称为手机号码最后4位。

UPDATE trans_eval SET issuer_name = MID(issuer_name,4,6) WHERE CHAR_LENGTH(issuer_name) = 11 AND issuer_name LIKE 1%;

2.由于误操作(MID(issuer_name,4,6)是中间的6位),需要数据回滚。

3.建立中间表(AND 1<>1 条件不符合建立空表)

CREATE TABLE tmp 
SELECT T2.REG_NO,T2.MOBILE,t1.`issuer_name`  FROM trans_eval  t1,member t2 WHERE t1.issuer_no =t2.`reg_no`  
 AND  MID(t2.`mobile`,4,6) =t1.`issuer_name`  AND 1<>1 GROUP BY T2.REG_NO,T2.MOBILE,t1.`issuer_name` ;

4.导入数据到中间表

INSERT INTO tmp 
SELECT T2.REG_NO,T2.MOBILE,t1.`issuer_name`  FROM trans_eval  t1,member t2 WHERE t1.issuer_no =t2.`reg_no`  
 AND  MID(t2.`mobile`,4,6) =t1.`issuer_name`  GROUP BY T2.REG_NO,T2.MOBILE,t1.`issuer_name` ;

5.数据恢复

SELECT * FROM tmp;
UPDATE tmp t1,trans_eval t2 SET t2.`issuer_name`=t1.`MOBILE` WHERE t1.`REG_NO`=t2.`issuer_no`;

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。