900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > mysql 中的case when 和ifnull判断

mysql 中的case when 和ifnull判断

时间:2023-12-25 10:45:41

相关推荐

mysql 中的case when 和ifnull判断

mysql 在查询和更新sql中都可以进行判断,下面根据我这两条sql看下case when和ifnull的用法:

case when

update t_tradefee set margin_profit = (case when market_price is null then (2.3-basic_fee)*remain_amount else (2.3-market_price)*remain_amount+margin_profit END),market_price = 2.3 where substring(cardno,1,3)='001' and trade_type = 1

更新过程中的判断,如果market_price为空则set margin_profit的值为(2.3-basic_fee)*remain_amount,否则 set margin_profit的值为2.3-market_price)*remain_amount+margin_profit

用法:case when… else… end

ifnull

select day(op_time) as day,sum(price) as price,IFNULL((select sum(price) from t_margin_profit where op_time<'-1-1 00:00:00' ),0) as oldfrom t_margin_profit where year(op_time)='' and month(op_time)='1' GROUP BY day(op_time)

查询中的判断赋值操作

用法:IFNULL(exp1,exp2) : 如果exp1的值不为空,取exp1的值,否则取exp2的值。

更新:

mysql select 中if判断第三种方式:

if(a,b,c)

如果表达式a的值为true,返回b,否则返回c

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。