MySQL 统计上一周从周一到周日的用户

这个功能按理说很常见,奇怪的是很难搜索到一个合适的。稍微整理了下,具体的就不展开了,注意这个表中的时间为毫秒,这条语句拷贝复制就能用。照顾大部分的无脑码农。

 SELECT
            case
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 0 then '0'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 1 then '1'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 2 then '2'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 3 then '3'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 4 then '4'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 5 then '5'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 6 then '6'
            end as name, count(id) as total
        FROM user_t as u WHERE (YEARWEEK(FROM_UNIXTIME(u.time_c/1000, '%Y-%m-%d')) = YEARWEEK(now())-1) and status = 1 group by
            case
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 0 then '0'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 1 then '1'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 2 then '2'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 3 then '3'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 4 then '4'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 5 then '5'
                when FROM_UNIXTIME(u.time_c/1000, '%w') = 6 then '6'
            end
        order by null;

你可能感兴趣的:(SQL)