[英]MySQL Select Distinct and count number of each distinct condition matching criteria
由于某些原因,我对此感到很麻烦,有人可以帮我吗?
在MySQL中,我有一个名为Appointment的表,其基本结构如下:
Table Appointment
Col | Type
--------------------------
id_appointment | int
fid_person | int
start_time | DATETIME
filled | int
我希望能够选择约会存在的所有日期,然后计算该日期未填写的约会的数量,以便我可以输出如下内容:
可用的约会
- 10/24/2014: 3 Appointments
- 10/25/2014: 1 Appointment
- 10/30/2014: 5 Appointments
到目前为止,我有:
SELECT DISTINCT DATE(start_time) as date FROM Appointment
这返回了我所有独特日子的列表,但是我似乎无法弄清其余的一切,我尝试了一个子查询,但似乎无法正确处理,因为我已经做了MySQL很久了
任何帮助表示赞赏! 谢谢!
如果我理解正确,那么这只是带有条件聚合的group by
查询:
select date(start_time), sum(filled = 0) as NumAvailable
from Appointment
group by date(start_time)
order by date(start_time);
使用GROUP BY
,它会自动隐含DISTINCT
。
SELECT DATE(start_time) AS date, COUNT(*)
FROM Appointment
WHERE filled = 0
GROUP BY date
这不会显示没有约会的日子。 如果需要,请使用Gordon Linoff的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.