如何计算特定时间段的值

时间:2017-06-02 14:27:36

标签: mysql sql

我有两个表,company(列idname)和post(列idcompany_id,{{1 }})。如果我想计算某段时间的帖子数量,我应该如何编写查询?

created

例如,

公司

SELECT c.id, c.name,count(p1.id)
FROM company c
JOIN (SELECT id, company_id, min(created) as startday, max(created) as endday from post) p
JOIN (select id, company_id, created from post) p1
ON p1.company_id=c.id and p1.created > endday and p1.created < startday
GROUP BY c.id

----------
id | name
-----------
 1 |   a
 2 |   b
 3 |   c    
-------------

我想得到像这样的结果

------
id|company_id|created
 1|  1       | 2017-01
 2|  1       | 2017-02
 3|  1       | 2017-02
 4|  2       | 2-17-01
---------

0 个答案:

没有答案