连接表,计数和日期

时间:2015-01-05 18:10:49

标签: mysql

我想加入两个表,并根据日期从一个表中检索一个计数。

我有一张表 access_points

access_points_id, access_points_name 

和另一个表 access_points_launched

access_points_launched_id, access_points_name, launched_date

我想返回所有access_points,并计算当前日期access_points_launched中的条目数:

Access Point, Launched Count, Date
WIFI_point, 2, 05/01/2015
FREE_INTERNET, 0, 05/01/2015
SPRINT_INTERNET, 5, 05/01/2015

我不希望伯爵包括任何其他日期。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以在子查询中获取聚合计数,并且可以使用access_points表执行left join以获得所需的结果。

select a.access_points_name, aggr.totalCount , curdate()
from access_points a
left join ( select access_points_name, count(*) as totalCount
            from access_points_launched
            where date(launched_date) = curdate() 
            group by access_points_name
          ) aggr
on aggr.access_points_name =  a.access_points_name