CASE BETWEEN声明

时间:2013-04-01 20:21:01

标签: php mariadb

我的数据库中有一个简单的日志表。对于每个页面,访问插入一行。现在我想总结今天和昨天的某一专栏。

我尝试了以下查询:

SELECT Sum(CASE 
             WHEN `time` > 1364767200 THEN `sqlquerycount` 
           end) AS `today`, 
       Sum(CASE 
             WHEN `time` BETWEEN 1364684400 AND 1364767200 THEN `sqlquerycount` 
           end) AS `yesterday` 
FROM   `log` 

以下PHP脚本的结果:

$sql = "SELECT 
        SUM(CASE WHEN `time` > ".strtotime("today")." THEN `sqlQueryCount` END) AS `today`,
        SUM(CASE WHEN `time` BETWEEN ".strtotime("yesterday")." AND ".strtotime("today")." THEN `sqlQueryCount` END) AS `yesterday`
    FROM `log`";

“昨天”保持为NULL,但我没有看到此查询有任何问题。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我敢打赌

$sql = "SELECT 
        SUM(CASE WHEN `time` > ".strtotime("today")." THEN `sqlQueryCount` ELSE 0 END) AS `today`,
        SUM(CASE WHEN `time` BETWEEN ".strtotime("yesterday")." AND ".strtotime("today")." THEN `sqlQueryCount` ELSE 0 END) AS `yesterday`
    FROM `log`";

...提供sqlQueryCount是您想要总和的列!