【发布时间】:2019-05-21 06:12:54
【问题描述】:
我有两个表,分别是“appointment”和“skills_data”。
约会表的结构是:
id_ap || ap_meet_date || id_skill || ap_status.
而ap_status的值有complete、confirm、cancel和miss。
而技能数据表包含两列,即:
id_skill || skill
我想获取每个条件的约会总数
ap_status = ('complete' and 'confirm'),
ap_status = 'cancel' and
ap_status = 'missed'
GROUP BY id_skill and year and
order by year DESC
我尝试了这个查询,它只计算了一个条件,但我想根据前面提到的 group by 和 order by 子句获得另外两个条件。
如果没有符合特定条件的记录(例如:某项技能2018年缺约0次),则应显示输出值为0表示零计数。
有人可以向我建议我是否应该实施多选查询或 CASE 子句来实现我的预期结果。我在约会表中有很多记录,并且想要一种有效的方式来查询我的记录。谢谢!
SELECT a.id_skill, YEAR(a.ap_meet_date) As year, s.skill,COUNT(*) as count_comp_conf
FROM appointment a,skills_data s WHERE a.id_skill=s.id_skill and a.ap_status IN ('complete', 'confirm')
GROUP BY `id_skill`, `year`
ORDER BY `YEAR` DESC
我的查询的输出:
id_skill | year | skill | count_comp_conf
-----------------------------------------
1 2018 A 20
2 2018 B 15
1 2019 A 10
2 2019 B 12
3 2019 C 10
我的预期输出应该是这样的:
id_skill | year | skill | count_comp_conf | count_cancel | count_missed
------------------------------------------------------------------------
1 2018 A 20 5 1
2 2018 B 15 8 0
1 2019 A 10 4 1
2 2019 B 12 0 5
3 2019 C 10 2 2
【问题讨论】:
标签: mysql