在以下情况下使用选择和大小写获取输出

时间:2018-09-12 09:54:37

标签: php

我正在尝试以以下格式显示输出:

enter image description here

错误

致命错误:mysqli_sql_exception未捕获:SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'附近使用minrange,以sum(case,当DATE(holddate)= CURDATE())作为holddate,以sum(case'at

代码:

<?php

$sql =

"select 
sum(case when DATE(reattemptdate) = CURDATE()) as minrange,
sum(case when DATE(holddate) = CURDATE()) as holddate,
sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_reattemptdate,
sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_holddate
from orders
 ";
$results = $db_handle->runSelectQuery($sql); 
$numrowsresult =$results[0]['count'];  
echo $numrowsresult;

?>

2 个答案:

答案 0 :(得分:1)

我需要这个:THEN 1 ELSE 0 END

select sum(case when DATE(reattemptdate) = CURDATE() THEN 1 ELSE 0 END) as reattemptdate,
       sum(case when DATE(holddate) = CURDATE() THEN 1 ELSE 0 END) as holddate,
       sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_reattemptdate,
       sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_holddate
from orders;

答案 1 :(得分:0)

您有一个CASE WHEN,但您从未指定THEN(和ELSE)。

因此语法缺少一些内容。