2013-04-01 99 views
0

我在我的數據庫中有一個簡單的日誌表。對於每個頁面訪問,插入一行。現在我想爲今天和昨天總結一個專欄。CASE BETWEEN聲明

我嘗試以下查詢:

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`"; 

「昨天」的剩餘空,但我沒有看到與此查詢任何問題。任何想法?

+0

你使用'mariadb'或'mysql'嗎? – Kermit

+0

我正在使用MariaDB。 – Zwirbelbart

+0

'SELECT COUNT(*)FROM log WHERE time BETWEEN 1364684400 AND 1364767200'的結果是什麼? – Kermit

回答

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是你想做的總和列!

+0

我對此表示滿意,因爲OP可能希望看到數字結果。 – Kermit

+0

+1。除了我的問題的評論,這似乎是一個解決方案。謝謝! – Zwirbelbart