我有一个用户每天输入数据的表格
我需要查询(在一个查询中)以获取每天,每月,每年和所有按用户分组输入的记录总数
任何建议??
username count(today) count(month) count(year) count(*)
user1 10 100 1000 2300
user2 5 70 730 1240
答案 0 :(得分:0)
Count,CASE和Group by将为您完成:
select user_name, sum(case when curdate()=date_time then 1 end) Today,
sum(case when Month(curdate())=Month(date_time) then 1 end) Month,
sum(case when year(curdate())=year(date_time) then 1 end) Year,
count(*)
from table1
group by user_name;