当前位置:   article > 正文

Mysql update多表更新即使用_mysql 多表update

mysql 多表update
1. inner join
UPDATE topic_relation r
INNER JOIN course c ON c.id = r.target_id
SET r.target_code = c.course_code
WHERE r.type = '1012005';

2.隐式

update test_user set login_code=SUBSTRING_INDEX(email,'@',1);
update province_channel_manager as a,test_user as b set a.login_code = b.login_code 
where a.channel_manager_num=b.telephone;

3.left join  此时product 中比 product_price多一条数据,该数据就是修改的数据

UPDATE product p LEFT JOIN product_price pp ON p.productid= pp.productid SET p.isdelete = 1 WHERE pp.productid IS NULL;
Query OK, 1 row affected (0.04 sec)

4.子查询

UPDATE product_price pp SET price=(SELECT price*0.8 FROM product WHERE productid = pp.productid);

5.同时更新多个字段

UPDATE product p INNER JOIN product_price pp ON p.productid= pp.productid SET pp.price = p.price * 0.8, p.dateUpdate = CURDATE()

给表同时添加多个字段
ALTER TABLE xxName
ADD COLUMN `xx_id` BIGINT DEFAULT NULL COMMENT 'xxid' AFTER `status`,
ADD COLUMN `info_id` BIGINT DEFAULT NULL COMMENT 'info信息id' AFTER `xx_id`;

原表—fund_product_info 

fund_codefund_manager_code
123450dk_001,dk_230,dk_589
011455dk_011,dk_589

关系数据移动到关系表—fund_product_manager_relation

fund_codefund_manager_code
123450dk_001
123450dk_230
123450dk_589
011455dk_011
011455dk_589
  1. -- 1
  2. drop table if exists test1;
  3. CREATE TABLE if not exists `test1` (
  4. `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  5. sort tinyint default null comment'对比id',
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB COMMENT='协助迁移产品数据';
  8. INSERT INTO `test1`(`id`, `sort`) VALUES (1, 0);
  9. INSERT INTO `test1`(`id`, `sort`) VALUES (2, 1);
  10. INSERT INTO `test1`(`id`, `sort`) VALUES (3, 2);
  11. INSERT INTO `test1`(`id`, `sort`) VALUES (4, 3);
  12. -- 2
  13. truncate table fund_product_manager_relation;
  14. -- 3
  15. insert into fund_product_manager_relation(fm_sort,fund_code,fund_name,fund_manager_code,fund_manager_name,current_manage)
  16. SELECT
  17. b.sort,
  18. a.fund_code,
  19. a.fund_name,
  20. SUBSTRING_INDEX( SUBSTRING_INDEX( a.fund_manager_code, ',', b.sort + 1 ), ',',- 1 ) fund_manager_code,
  21. '',
  22. CASE
  23. deleted
  24. WHEN 1 THEN
  25. 'N' ELSE 'Y'
  26. END current_manage
  27. FROM
  28. fund_product_info AS a
  29. LEFT JOIN test1 AS b ON ( char_length( a.fund_manager_code ) - char_length( REPLACE ( a.fund_manager_code, ',', '' ) ) ) + 1 > b.sort
  30. WHERE
  31. a.fund_manager_code IS NOT NULL;
  32. -- 4
  33. update fund_product_manager_relation as a, fund_manager_info as b set a.fund_manager_name= b.fund_manager_name where a.fund_manager_code=b.fund_manager_code;
  34. -- 5
  35. drop table test1;

主要  辅助表test1+  substring_index(str,delim,count)使用!

select SUBSTRING("123.456.78",-2,3)78
select SUBSTRING("123.456.78",2,3)23.
select substring_index("123.456.78",'.',3)123.456.78
select substring_index("123.456.78",'.',1)
正数从左往右数分隔符,取分隔符左侧内容
123  
select substring_index("123.456.78",'.',-1)
从从右往左数分隔符,取分隔符右侧内容
78

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/142063
推荐阅读
相关标签
  

闽ICP备14008679号