MySQL:从“数据列表”中选择

时间:2013-05-31 13:15:19

标签: mysql

对于某些计算,我需要过去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`

但是我无法弄清楚这样做的正确语法。它甚至可能吗?如果是的话,怎么样?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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`

答案 2 :(得分:0)

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

然后,您可以根据需要将现有表格加入临时表格。