SQL-Update语句

SQL-Update语句

根据 levelCodeName 更新  levelCode

 建表语句

SQL-Update语句
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

 

上一篇:Python 新手常犯错误(第二部分)


下一篇:SQL Server 生成字典表