2013-05-31 111 views
0

對於某些計算,我需要過去30天的某些表條目的總和。我的想法是做這樣的事情:MySQL:從「數據列表」中選擇

SELECT 
    a.`date`, 
    (SELECT COUNT(*) FROM `subtable1` AS b WHERE b.`date` = a.`date`) AS `sum1`, 
    (SELECT COUNT(*) FROM `subtable2` AS c WHERE c.`date` = a.`date`) AS `sum2`, 
    (SELECT COUNT(*) FROM `subtable3` AS d WHERE d.`date` = a.`date`) AS `sum2` 
FROM 
    ("31.05.2013", "30.05.2013", "29.05.2013") AS `date` 

但我無法找出正確的語法來做到這一點。它甚至有可能嗎?如果是,如何?

+0

您可以使用UNION。您不能在from子句中使用日期值。 –

+0

不,我只需要遍歷通過php生成的「一些日期值」。我不想爲每個日期觸發單個查詢,我認爲最好在單個查詢中執行此操作。 –

+2

我甚至不明白你假裝要做什麼 – AAlferez

回答

1

試試這個:

SELECT 
    `date`, 
    SUM(IF(za_type=1,nb,0)) as sum1, 
    SUM(IF(za_type=2,nb,0)) as sum2, 
    SUM(IF(za_type=3,nb,0)) as sum3 
FROM (
    SELECT b.`date`,1 as za_type, COUNT(*) as nb FROM `subtable1` AS b WHERE b.`date` IN ('31-05-2013','30-05-2013','29-05.2013') UNION 
    SELECT c.`date`,2 as za_type, COUNT(*) as nb FROM `subtable2` AS c WHERE c.`date` IN ('31-05-2013','30-05-2013','29-05.2013') UNION 
    SELECT d.`date`,3 as za_type, COUNT(*) as nb FROM `subtable3` AS d WHERE d.`date` IN ('31-05-2013','30-05-2013','29-05.2013') 
) as tmp 
GROUP BY 
    `date` 

UPDATE:如果需要最後30天,你可以添加此條件的date >= NOW() - INTERVAL 30 DAY代替date IN (..)

UPDATE2:使用了新的要求(該查詢最後3天):

SELECT 
    za_day, 
    (SELECT COUNT(*) FROM subtable1 s WHERE s.date = za_day) as sum1, 
    (SELECT COUNT(*) FROM subtable2 s WHERE s.date = za_day) as sum2, 
    (SELECT COUNT(*) FROM subtable3 s WHERE s.date = za_day) as sum3 
FROM (
    SELECT DATE(NOW()) - INTERVAL 1 DAY as za_day UNION 
    SELECT DATE(NOW()) - INTERVAL 2 DAY as za_day UNION 
    SELECT DATE(NOW()) - INTERVAL 3 DAY as za_day 
) as td 
+0

但是這個解決方案不會錯過一天,當某個特定日期沒有數據時? –

+0

我不知道這是一個需求...我會想到一個解決方案 – Stephan

+0

請查看我的查詢 – Stephan

1

從@Vivek使用UNION的提示帶來了正確的方法。解決的辦法是:

SELECT 
`current_date`, 
(SELECT COUNT(*) FROM `subtable1` AS b WHERE b.`date` = `current_date`) AS `sum1`, 
(SELECT COUNT(*) FROM `subtable2` AS c WHERE c.`date` = `current_date`) AS `sum2`, 
(SELECT COUNT(*) FROM `subtable3` AS d WHERE d.`date` = `current_date`) AS `sum3` 
FROM 
(
    SELECT "31.05.2012" AS `current_date` 
    UNION SELECT "30.05.2012" AS `current_date` 
    UNION SELECT "29.05.2012" AS `current_date` 
) AS `dates` 

編輯

所以,最終的查詢看起來像這樣和計數所有廣告點擊,廣告印象和一些其他的東西在過去的30天(時間戳是由一些產生PHP代碼)。

SELECT 
     `current_timestamp`, 
     (
      SELECT 
       COUNT(*) 
      FROM 
       `ad_clicks` AS a 
      WHERE 
       FLOOR(a.`timestamp`/86400) * 86400 = `current_timestamp` 
     ) AS `ad_click_count`, 
     (
      SELECT 
       COUNT(*) 
      FROM 
       `ad_impressions` AS b 
      WHERE 
       FLOOR(b.`timestamp`/86400) * 86400 = `current_timestamp` 
     ) AS `ad_impression_count`, 
     (
      SELECT 
       COUNT(*) 
      FROM 
       `stand_touches` AS c 
      WHERE 
       FLOOR(c.`timestamp`/86400) * 86400 = `current_timestamp` 
     ) AS `stand_touch_count`, 
     (
      SELECT 
       COUNT(*) 
      FROM 
       `stand_url_clicks` AS d 
      WHERE 
       FLOOR(d.`timestamp`/86400) * 86400 = `current_timestamp` 
     ) AS `stand_url_call_count` 
    FROM 
     (  
      SELECT "1369958400" AS `current_timestamp` UNION 
      SELECT "1369872000" AS `current_timestamp` UNION 
      SELECT "1369785600" AS `current_timestamp` UNION 
      SELECT "1369699200" AS `current_timestamp` UNION 
      SELECT "1369612800" AS `current_timestamp` UNION 
      SELECT "1369526400" AS `current_timestamp` UNION 
      SELECT "1369440000" AS `current_timestamp` UNION 
      SELECT "1369353600" AS `current_timestamp` UNION 
      SELECT "1369267200" AS `current_timestamp` UNION 
      SELECT "1369180800" AS `current_timestamp` UNION 
      SELECT "1369094400" AS `current_timestamp` UNION 
      SELECT "1369008000" AS `current_timestamp` UNION 
      SELECT "1368921600" AS `current_timestamp` UNION 
      SELECT "1368835200" AS `current_timestamp` UNION 
      SELECT "1368748800" AS `current_timestamp` UNION 
      SELECT "1368662400" AS `current_timestamp` UNION 
      SELECT "1368576000" AS `current_timestamp` UNION 
      SELECT "1368489600" AS `current_timestamp` UNION 
      SELECT "1368403200" AS `current_timestamp` UNION 
      SELECT "1368316800" AS `current_timestamp` UNION 
      SELECT "1368230400" AS `current_timestamp` UNION 
      SELECT "1368144000" AS `current_timestamp` UNION 
      SELECT "1368057600" AS `current_timestamp` UNION 
      SELECT "1367971200" AS `current_timestamp` UNION 
      SELECT "1367884800" AS `current_timestamp` UNION 
      SELECT "1367798400" AS `current_timestamp` UNION 
      SELECT "1367712000" AS `current_timestamp` UNION 
      SELECT "1367625600" AS `current_timestamp` UNION 
      SELECT "1367539200" AS `current_timestamp` UNION 
      SELECT "1367452800" AS `current_timestamp` 
     ) AS `timestamps` 
+1

+1我有完全相同的方法 – Stephan

0

您可以設置一個臨時表並填寫您想要的數據。

然後,您可以將您現有的表格與臨時表格結合起來。