update case when的使用

示例:根据年龄确定组别

-- 更新
update `act_txtx_players_mf` as pmf inner join `act_txtx_players` as p on pmf.players_id=p.id set pmf.act_group=(
	case 
	when p.age<7 then 1 
	when p.age<10 then 2 
	else 3 end
) WHERE pmf.mid=2


-- 查询
select pmf.players_id,pmf.mid,p.age,
(
	case 
	when p.age<7 then 1 
	when p.age<10 then 2 
	else 3 end
) as actgroup 
from `act_txtx_players_mf` as pmf 
inner join `act_txtx_players` as p on pmf.players_id=p.id 
where pmf.mid=2

你可能感兴趣的:(SQL技巧)