我想计算直到上个月注册完成的月数。我将查询写成
SELECT count(MONTH(`regdate`) < MONTH(CURDATE()) AND YEAR(`regdate`) <= YEAR(CURDATE())) FROM `client_details` WHERE hospitalid='apollo123'
答案 0 :(得分:0)
也许你想要这个:
select month(regdate), count(*)
from client_details
where regdate < curdate()
and hospitalid = 'apollo123'
group by month(regdate);
这将在curdate()
之前的每个月为您提供当月发生的注册计数。
答案 1 :(得分:0)
SELECT count (*)from (
select month(regdate)
from client_details
where regdate < curdate()
and hospitalid = 'apollo123'
group by month(regdate));
这将为您提供截至上个月注册时的月数。
答案 2 :(得分:-2)
一个解决方案是
select count(distinct(cast(MONTH(regdate) as nvarchar(10)) + cast(year(regdate) as nvarchar(10)))) from client_details where hospitalid = 'apollp123' and regdate < DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0)
我在Sql Server上测试过这个,你应该调整mySql的代码。