直接看代码吧。
① 第一种 将count()取别名c,这种写法是错误的,因为下面的sum/c SQL不认识
② 第二种 将count() 直接写入到查询的元素中的部分,这样直接SQL就查出来了
1、
select
user_name,
count(distinct(DATE_FORMAT(create_time,'%Y-%m-%d'))) as c
sum(user_count) as userNum,
sum(user_count)/c as dayNum
from
A
group by user_name;
2、
select
user_name,
sum(user_count) as userNum,
sum(user_count)/count(distinct(DATE_FORMAT(create_time,'%Y-%m-%d'))) as dayNum
from
A
group by user_name;