SQL-Update语句

 

 一、根据 levelCodeName 更新  levelCode

建表语句

CREATE TABLE `sy_cd_ms_tag_level` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `bigId` int(10) NOT NULL COMMENT '大类编码',
  `bigName` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '大类名称',
  `fieldCode` int(2) DEFAULT NULL COMMENT '来源字段类型',
  `fieldCodeName` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '范围类型名称',
  `levelScore` int(10) NOT NULL,
  `levelCodeName` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '相关度等名称',
  `levelCode` int(2) DEFAULT NULL COMMENT '相关度等级代码',
  PRIMARY KEY (`id`),
  KEY `bigId_idx` (`bigId`),
  KEY `fieldCode_idx` (`fieldCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
View Code

更新语句

UPDATE sy_cd_ms_tag_level t1,(
SELECT id,levelCodeName,
CASE WHEN levelCodeName = '五星级'  THEN '5' 
WHEN levelCodeName = '四星级'  THEN '4' 
WHEN levelCodeName = '三星级'  THEN '3' 
WHEN levelCodeName = '二星级'  THEN '2' 
WHEN levelCodeName = '一星级'  THEN '1' 
WHEN levelCodeName = '零星级'  THEN '0' 
END as levelCode
FROM sy_cd_ms_tag_level
)t2
set t1.levelCode=t2.levelCode
where t1.id=t2.id

 

二、industryCode有的有默认值,没有默认值的根据表sq_oa_product_tag的industryCode值来更新表sy_cd_ms_ind_chain_macro_rela_2

insert into sy_cd_ms_ind_chain_macro_rela_2 (id,industryCode)
select b.id,a.labelId as industryCode from sq_oa_product_tag a 
inner join 
(select * from sy_cd_ms_ind_chain_macro_rela_2 where industryCode is null) b
on a.label = b.industryName
on duplicate key update industryCode=values(industryCode)

 

posted @ 2022-01-26 09:51  奇遇yms  阅读(221)  评论(0编辑  收藏  举报