[本文出自天外归云的博客园]
问题
现在要写一个查询,返回指定时间范围内各项目的项目名以及支付记录数
表结构
项目支付记录project_purchase表
project
|
...
|
created_at
|
test_project
|
...
|
2021-03-12
|
项目信息project_info表
project
|
name
|
test_project
|
测试项目
|
分析
需要对项目支付记录表和项目信息表进行联结查询,并对查询结果按项目进行分组,从而获得项目的支付记录数
SQL
select name,count(*) as num from project_purchase left join project_info on project_purchase.project = project_info.project where project_purchase.created_at between '2021-03-20' and '2021-07-20' and name <> "" group by project_purchase.project
其中count(*)函数和group by经常是成对使用,表示按某列对查询结果进行分组计数