mysql的when case使用

aaa_id中有3,删除2及前后的“,”,没有3,将2替换为3

SELECT
t.aaa_id as aaa_id_org
,locate(‘3‘,t.aaa_id)  //是否包含改字符,包含返回下标,不包含=0
,case when locate(‘3‘,t.aaa_id)>0
then REPLACE(REPLACE(t.aaa_id,‘,2‘,‘‘),‘2,‘,‘‘)  // 双重替换
ELSE REPLACE(t.aaa_id,‘2‘,‘3‘)
end as test
FROM
TAB_STATUS t
WHERE
id = ‘31325b2f2d614d12bc7307c47‘
;

 

UPDATE TAB_AAA t
set aaa_id = case when locate(‘3‘,t.aaa_id)>0
then REPLACE(REPLACE(t.aaa_id,‘,2‘,‘‘),‘2,‘,‘‘)
ELSE REPLACE(t.aaa_id,‘2‘,‘3‘) end
where id = ‘31325b2f2d614d12bc73‘
;

 

//聚合统计每小时的请求量

SELECT
DATE_FORMAT( end_time, ‘%Y-%m-%d %H‘ ) as end_time,proxy_type,`proxy_id`,   
SUM(DISTINCT req_count) as req_count
FROM
tab_test
GROUP BY
end_time,proxy_id,proxy_type
ORDER BY start_time DESC

 

mysql的when case使用

上一篇:查看Mysql版本


下一篇:初始数据库