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`;

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/4647.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论