地方门户网站规划,嘉兴城乡建设局网站,义乌建设局网站打不开,东莞建站模板代理postgresql 条件表达式 简单CASE表达式搜索CASE表达式缩写函数nullif函数示例 coalesce函数 总结 简单CASE表达式
语法如下
case 表达式when 值1 then 结果1when 值2 then 结果2else 默认值
end;select
e.first_name ,
e.last_name ,
case e.department_id
when 90 then 管… postgresql 条件表达式 简单CASE表达式搜索CASE表达式缩写函数nullif函数示例 coalesce函数 总结 简单CASE表达式
语法如下
case 表达式when 值1 then 结果1when 值2 then 结果2else 默认值
end;select
e.first_name ,
e.last_name ,
case e.department_id
when 90 then 管理
when 60 then 开发
else 其他
end as 部门
from employees e ;搜索CASE表达式
case when 条件1 then 结果1when 条件2 then 结果2else 默认结果
end根据薪水的范围将员工的收入分为高中低三个档次
select
e.first_name ,
e.last_name,
e.salary ,
case when e.salary 5000 then 低收入when e.salary between 5000 and 10000 then 中等收入else 高收入
end as 收入
from public.employees e ;缩写函数
nullif函数
语法如下
nullif(表达式1,表达式2)NULLIF 函数包含 2 个参数如果第一个参数等于第二个参数返回 NULL否则返回第 一个参数的值,它可以使用等价的 CASE 表达式表示为
CASEWHEN expression_1 expression_2 THEN NULLELSE expression_1
END示例
select nullif(1, 1), nullif(a, b);nullif 还有常见的用法就是除数为0的错误
select 1 / nullif(0,0) as t;coalesce函数
coalesce (表达式1,表达式2,表达式3)COALESCE 函数接受多个参数并且返回第一个非空的参数值如果所有参数都为空值 返回 NULL 值。它可以使用等价的 CASE 表达式表示为
casewhen 表达式1 is not null then 表达式1when 表达式2 is not null then 表达式2when 表达式3 is not null then 表达式3
end-- 查询佣金比率为空的数据显示为 0
select
e.first_name ,
coalesce(e.commission_pct,0) as commissionPct
from cps.public.employees e;总结