我有两个表,company
(列id
,name
)和post
(列id
,company_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
---------