sql语句 根据时间分组查询

1.根据时间分组,返回月份对应的人数

<!-- sex传入性别,根据时间分组,返回月份对应的人数 -->
select concat(month(registTime),'月') months ,count(*) count from user where sex='男' GROUP BY month(registTime)

在这里插入图片描述

2.根据城市分组,返回城市对应的人数

<!-- sex传入性别,根据城市分组,返回城市对应的人数 -->
select city as name,count(*) as value from user where sex='性别' group by city
<!--拼接字符串         concat(值,”拼接的字符“)  -->

sql语句 根据时间分组查询_第1张图片

3.使用sql获取当前时间单独的年、月、日

select year(createtime) from life_unite_product    --取时间字段的年值

select month(createtime) from life_unite_product   --取时间字段的月值

select day(createtime) from life_unite_product     --取时间字段的天值

你可能感兴趣的:(sql)