组合两个按日期分组的select语句,返回两列数据

时间:2016-11-24 17:18:49

标签: mysql

这似乎是一件容易的事,但我的基本sql知识让我失望,因为我还在学习。

基本上,我试图合并:

SELECT DATE(created) DATE, COUNT(DISTINCT created) newpost FROM surveys
WHERE created >= Last_day(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND created < last_day(CURRENT_DATE) + INTERVAL 1 DAY GROUP BY DATE(created);

SELECT DATE(TIMESTAMP) DATE,subs FROM trafficstats
WHERE TIMESTAMP >= LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND TIMESTAMP < LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY;

在一个查询中,将按日期分组的数据返回到另外两列 - newpostssubs

我尝试使用UNION,它似乎没有给我我想要的输出。它将数据合并为一列(newpost),也没有按日期分组。

我仍然很擅长撰写MySQL个问题,而且我试图寻找无济于事的答案。希望在这里寻求比我聪明的人的知识。

2 个答案:

答案 0 :(得分:3)

您可以使用JOIN

    select t1.DATE, t1.newpost, t2.subs 
    from (
      SELECT DATE(created) DATE, COUNT(DISTINCT created) newpost 
      FROM surveys 
      WHERE created >= Last_day(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
      AND created < last_day(CURRENT_DATE) + INTERVAL 1 DAY 
      GROUP BY DATE(created)
    ) t1
    left join  (
      SELECT DATE(TIMESTAMP) DATE, subs 
      FROM trafficstats 
      WHERE TIMESTAMP >= LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
      AND TIMESTAMP < LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY
    ) t2 on t1.DATE = t2.DATE

答案 1 :(得分:1)

我猜你想要每个不同的日期有一行,显示两个不同的计数值。

这种查询比初看起来略显琐碎,因为两个摘要查询可能有不同的日期集。

因此,您需要从子查询开始,该子查询产生所有可能感兴趣的日期。然后,您需要将每个摘要查询LEFT JOIN加入其中。您必须使用LEFT JOIN而不是普通的内部JOIN,因为当LEFT JOIN与左侧的任何行不匹配时,它不会抑制连接右侧的行。

这里是:

所有日期。请注意UNION操作是一个setwise(重复删除)并集操作。

            SELECT DISTINCT DATE(created) DATE FROM newpost
             WHERE created >= Last_day(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
              AND created < last_day(CURRENT_DATE) + INTERVAL 1 DAY
             UNION
            SELECT DISTINCT DATE(TIMESTAMP) DATE FROM trafficstats 
             WHERE TIMESTAMP >= LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
               AND TIMESTAMP < LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY

然后你需要两个汇总子查询。第一个就是这个。请注意,我已将COUNT(DISTINCT created)更改为COUNT(*),因为我不了解DISTINCT背后的逻辑。一个帖子可以有多行吗?你用时间戳告诉他们了吗?如果每个帖子都有一行,则应COUNT(*)

           SELECT DATE(created), COUNT(*) newposts
             FROM newpost
            GROUP BY DATE(created)

第二个总结就是这个。再次,我计算了行数。

           SELECT DATE(TIMESTAMP), COUNT(*) subs
             FROM trafficstats
            GROUP BY DATE(TIMESTAMP)

最后,像这样加入这三个子查询。您可以从第一个子查询中获取日期,并从后两个子查询中获取摘要的日期信息。

     SELECT dates.DATE, posts.newposts, subs.subs
       FROM ( /* date subquery */ ) dates
       LEFT JOIN ( /* posts subquery */ ) posts ON dates.DATE = posts.DATE
       LEFT JOIN ( /* subs subquery */ ) subs ON dates.DATE = subs.DATE
      ORDER BY dates.DATE

全部放在一起:

     SELECT dates.DATE, posts.newposts, subs.subs
       FROM (
            SELECT DISTINCT DATE(created) DATE FROM newpost
             WHERE created >= Last_day(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
              AND created < last_day(CURRENT_DATE) + INTERVAL 1 DAY
             UNION
            SELECT DATE(TIMESTAMP) DATE FROM trafficstats 
             WHERE TIMESTAMP >= LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH 
               AND TIMESTAMP < LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY
            ) dates
       LEFT JOIN (
                SELECT DATE(created), COUNT(*) newposts
                  FROM newpost
                 GROUP BY DATE(created)
            ) posts ON dates.DATE = posts.DATE
       LEFT JOIN (
                   SELECT DATE(TIMESTAMP), COUNT(*) subs
                     FROM trafficstats
                    GROUP BY DATE(TIMESTAMP)
            ) subs ON dates.DATE = subs.DATE
      ORDER BY dates.DATE