0

对于某些计算,我需要过去 30 天的一些表条目的总和。我的想法是做类似的事情:

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`

但我想不出正确的语法来做到这一点。甚至可能吗?如果是,如何?

4

3 回答 3

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`

更新:如果您需要过去 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
于 2013-05-31T13:21:24.913 回答
1

UNION来自@Vivek的提示是正确的。解决方案是:

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`
于 2013-05-31T15:16:13.243 回答
0

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

然后,您可以根据需要将现有桌子与临时桌子连接起来。

于 2013-05-31T15:16:28.627 回答