1)更新
update table
set 字段1=case
when 条件1 then 值1
when 条件2 then 值2
else 值3
end
where ……
2)查询
select 字段1, 字段2,
case 字段3
when 值1 then 新值
when 值2 then 新值
end as 重新命名字段3的名字
from table
where ……
order by ……
查询
select a.depr_name,b.clerk_id,b.salary,
CASE b.position WHEN 'java' THEN
b.salary * 0.1
WHEN b.position ='node' THEN
b.salary * 0.2
WHEN b.position ='php' THEN
b.salary * 0.3
ELSE
b.salary
END as increase_salary
from dept a,clerk b where a.dept_id = b.dept_id
更新
update clerk set salary = CASE position
WHEN 'java' THEN
salary * 2
WHEN 'node' THEN
salary *3
WHEN 'php' THEN
salary * 4
ELSE
salary
END
;
本文展示了SQL语言中如何使用CASE语句进行复杂的数据更新和查询操作。更新部分涉及根据不同条件设置字段值,查询部分则演示了如何在SELECT语句中重命名和计算字段值,特别是根据员工职位调整薪水的计算方法。
902





