带有*运算符解释的MySQL CASE语句语法

时间:2016-09-25 08:45:49

标签: mysql sql case

我有一个CASE语句,如下所示

        case
            when ph.term_start_date > x.year1 then 0
            DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
        end days_remaining_year1,
        case
            when ph.term_start_date > x.year1 then 0
            DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
        end *(ph.amount + ifnull(y.credit,0))/DATEDIFF(ph.term_end_date, ph.term_start_date) as deferral_year1,

我需要了解最后一行*关键字之后END的含义。我认为在结束关键字之后你必须指定一个将出现在结果集

中的列
Case 
When ( something )
end ( return column )

1 个答案:

答案 0 :(得分:2)

*乘法运算符

Doc:MySQL Arithmetic Operators

前面的CASE返回一个值,然后乘以:

(ph.amount + ifnull(y.credit,0))/DATEDIFF(ph.term_end_date, ph.term_start_date)

此结果将在deferral_year1

中返回

编辑 :回复评论

我偷偷摸摸你的2 CASE个陈述,他们看起来对我不正确。您的查询是否正确执行?

看起来在DATEDIFF之前缺少else,应该是这个IMO

    case
        when ph.term_start_date > x.year1 then 0
        else DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
    end

就像你说的那样ph.term_start_date > x.year1它应该返回0, else 它会返回DATEDIFF的结果。