decode只能做等值的,case when可以做区间的,decode能做到的,case when也能做到。
效率方面,个人觉得两者效率差不多。
但decode的语法要简单些,但它只能做等值的比较
case when end 能做条件的判断
select case when b.total_fee is null then 0 else b.total_fee end from t_pm_billing_stat b
select a.area_name, decode(b.billing_amount,null,0 ,b.billing_amount) from t_pm_billing_stat b
本文对比了SQL中DECODE函数与CASE WHEN语句的功能及使用场景。DECODE适用于简单的等值判断,语法简洁;CASE WHEN则支持更复杂的区间判断与条件分支,灵活性更高。两者在大多数情况下性能相近。
976

被折叠的 条评论
为什么被折叠?



