我正在尝试制作一个计算会员支出的选择.
我的方法非常简单.
SELECT month(payments.timestmap) ,sum(if(payments.amount>=29.95,4,0)) As Tier4 ,sum(if(payments.amount>=24.95<=29.94,3,0)) As Tier3 ,sum(if(payments.amount>=19.95<=24.94,2,0)) As Tier2 FROM payments GROUP BY month(payments.timestamp)
以上不起作用,因为MySQL没有评估条件的第二部分.顺便说一下,它不会导致语法错误,选择将返回结果.
在上面之前我尝试了我所假设的工作,就像" amount between 24.94 AND 29.94
"这导致了一个错误.所以我试过" amount >= 24.94 AND <= 29.94
"
那么可以在MySql中使用IF进行范围比较吗?
表达式的第二部分在您使用时评估AND
-
SELECT month(payments.timestmap) ,sum(if(payments.amount>=29.95,4,0)) As Tier4 ,sum(if(payments.amount>=24.95 AND payments.amount<=29.94,3,0)) As Tier3 ,sum(if(payments.amount>=19.95 AND payments.amount<=24.94,2,0)) As Tier2 FROM payments GROUP BY month(payments.timestamp)
我不完全确定该between
条款为什么不适合你,但上面应该做的工作.