SELECT userId,DATE_FORMAT(paidTime,"%Y%m-01")
from learn.order_info_utf
GROUP BY userId,DATE_FORMAT(paidTime,"%Y%m-01")
SELECT t1.m,count(t1.m),count(t2.m) FROM (
SELECT userId,DATE_FORMAT(paidTime,"%Y-%m-01") as m
from learn.order_info_utf
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 learn.order_info_utf
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)
GROUP BY t1.m
*注意【%Y-%m-01】之间的【-】
create algorithm = undefined view tmp
as
SELECT userId,DATE_FORMAT(paidTime,"%Y%m-01")
from learn.order_info_utf
GROUP BY userId,DATE_FORMAT(paidTime,"%Y%m-01")
SELECT t1.m,count(t1.m),count(t2.m) FROM tmp as t1
left JOIN tmp as t2
ON t1.userId = t2.userId and t1.m = DATE_SUB(t2.m,INTERVAL -1 MONTH)
GROUP BY t1.m