2013-05-31 112 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

您可以设置一个临时表并填写您想要的数据。

然后,您可以将您现有的表格与临时表格结合起来。