在同一个表中获取两个查询(按日期分组)

时间:2016-02-23 10:25:36

标签: mysql sql

我正在尝试使用MySQL数据库的第一个选择语句,当你不知道要谷歌搜索的内容时,这很难。

所以这就是我想要做的,我有两个问题:

select date(server_time) as d, count(*) as x
from table_A, table_B 
where date(server_time) >= '2015-07-17'  
and table_A.id = table_B.id    
and idname is not null
and idurl is not null        
group by date(server_time)

 select date(server_time) as d, count(*) as y
 from table_A 
 where date(sever_time) >= '2015-07-17'
 group by date(server_time)

我想要实现的是将两个计数列与日期结合起来:

d               x     y
-------------|-----|-------
2015-07-17   |  3  |  20
2015-07-18   |  2  |  50
2015-07-19   |  7  |  10
-------------|-----|-------

我一直在玩UNIONSELECT(SELECT count)等,但没有运气。

1 个答案:

答案 0 :(得分:3)

SELECT date(server_time) AS d, 
       count(*) AS x,
       (SELECT y 
        FROM (SELECT date(server_time) AS d, 
                     id,
                     count(*) AS y
              FROM table_A 
              WHERE date(sever_time) >= '2015-07-17'
              GROUP BY date(server_time)) sub
        WHERE SUB.id = a.id) AS y
FROM table_A a
INNER JOIN table_B b ON a.id = b.id
WHERE date(server_time) >= '2015-07-17'
AND idname IS NOT NULL
AND idurl IS NOT NULL      
GROUP BY date(server_time)