从时间戳列中选择特定的月份和年份记录mysql

时间:2019-05-19 14:32:01

标签: mysql

我想做的是选择并计算特定月份和年份的患者诊断数。我已经选择并计算了诊断,我的问题是如何在特定的月份和年份中选择此诊断。

我的查询      SELECT diagnosis, count(diagnosis) as total from patient_records where deleted_at is null group by diagnosis

表结构 Patient_records

#   Name    Type    Collation   Attributes  Null    

1   idPrimary   int(10)     UNSIGNED    No

2   diagnosis   varchar(191)    utf8_unicode_ci     

3   remarks text    utf8_unicode_ci     Yes NULL             

4   created_at  timestamp           Yes NULL             

5   updated_at  timestamp           Yes NULL             

6   deleted_at  timestamp           Yes NULL             

7   patient_id  int(10)     UNSIGNED    Yes NULL             

请帮助我成为一个初学者 我脑海中的查询就是这样

 SELECT diagnosis, count(diagnosis) as total from patient_records where 
 deleted_at is null and updated_at = month(5)&year(2019) group by 
 diagnosis

1 个答案:

答案 0 :(得分:0)

SELECT 
  diagnosis, 
  count(diagnosis) as total,
  DATE_FORMAT(created_at, '%m') As month, 
  YEAR(created_at) as Year 
from patient_records 
where deleted_at is null 
group by diagnosis