mysql实战

秦路老师授课,mysql实战部分

use dataanalyst;

SELECT * FROM dataanalyst.orderinfo;

SET SQL_SAFE_UPDATES = 0;

# 将原来的反斜线更改为-

update orderinfo set paidTime = replace(paidTime,'/','-') where paidTime is not null;

# 更新字符串为datatime格式

update orderinfo set paidtime=str_to_date(paidtime,'%Y-%m-%d %H:%i') where paidtime is not null;

# 统计不同月份的下单人数

# 有几个需要注意的点:

# 当有不同的年份时,使用DATE_FORMAT(date,format)

# count()时需要注意有没有重复值

# 未支付isPaid的月份为00,是脏数据,要过滤掉

select month(PaidTime) month,count(distinct(userID)) '总数'

from orderinfo

where isPaid = '已支付'

group by month(PaidTime);

# 统计用户3月份的回购率和复购率

# 复购率:本月消费人数中,有多少人是消费了一次以上

# 回购率:上个月购买,这个月依旧购买

# 编程的时候可以先从简单的写起,一边写一边运行一边改

# 统计所有ID总共有多少,

select count(cu), count(if (cu>1, 1, null)) as "大于一次" ,(count(if (cu>1, 1, null))/count(cu)) "复购率" from(

select userID, count(userID) as cu from orderinfo

where isPaid = "已支付" and month(PaidTime) = 03

# 把多次购买和一次购买区分开

group by userID) t

# 都有谁购买过了,购买了几次

# 回购率

select count(*),count(t3.userID) from(

(select * from orderinfo where month(PaidTime) = 03) a,

(select * from orderinfo where month(PaidTime) = 04) b

)where a.userID = b.userID

select  o.userID ,SEX, COUNT(1) from orderinfo o

inner join(

select * from userinfo where sex <> '') t

on o.userID = t.userID

GROUP BY orderID, sex

select count(t1.m), count(t2.m) from

(select userID, date_format(paidTime,'%Y-%m-01') as m from orderinfo

where isPaid = '已支付'

group by userId,date_format(paidTime,'%Y-%m-01')) t1

left join

(select userID, date_format(paidTime,'%Y-%m-01') as m from orderinfo

where isPaid = '已支付'

group by userId,date_format(paidTime,'%Y-%m-01')) t2

on t1.userID = t2.userID

and t1.m = date_sub(t2.m, interval 1 month) # t1的月份= t2的月份 - 一个月

group by t1.m #统计3月份的回购率,以3月份为主要

你可能感兴趣的:(mysql实战)