SQl if where子句中的语句

时间:2014-11-20 19:00:09

标签: mysql sql where

如何在我的陈述中添加底线?

SELECT
AgentID,
PaymentAmount
FROM Table A

WHERE 
DATEOFPAYMENT 

GROUP BY 
CollectorNumber

ORDER BY 
CollectorNumber,
LastName,
DateofPayment,
PaymentAmount

  

如果DATEOFPAYMENT =昨天,那么PaymentAmount else 0为'前一天'

  

如果DATEOFPAYMENT = current_Month,那么PaymentAmount else 0为' MTD'

  

如果DATEOFPAYMENT = current_Year则PaymentAmount else 0为' YTD

1 个答案:

答案 0 :(得分:0)

我们可以研究CASE syntax from here , it would be, roughly (I am not acquainted with MySQL date formats , so please look that up )

SELECT AgentID, PaymentAmount FROM Table A

WHERE CASE DATEOFPAYMENT 
 WHEN subdate(currentDate, 1)  THEN PaymentAmount  
 WHEN month() THEN PaymentAmount 
 WHEN year()  THEN PaymentAmount 
 ELSE 0
END

GROUP BY CollectorNumber

ORDER BY CollectorNumber, LastName, DateofPayment,
PaymentAmount

这几乎是伪代码,所以请修改语法。但案例陈述是这样的。