mysql统计用户的预约量 昨天 上周 总金额 总销量

思路 先统计所有用户 并使用left join 连接用户 昨天的销量(按用户id统计昨天销量)sql on 用户id=用户id ,其他的上周销量统计同理连接 返回最终每个用户的数据 并可以根据 销量排序 

 

 

select  a.*,IFNULL(b.last_day_num,0) as last_week_num ,IFNULL(c.last_day_num,0) as last_week_num ,

IFNULL(d.total_num,0) as order_num,IFNULL(d.total_price,0) as order_price from (

select merchants_id,merchants_name,merchants_city,merchants_country from tst_merchants

where is_delete='0'

<if test=' merchants_name != null and merchants_name != "" '>

and merchants_name like concat('%',#{merchants_name},'%')

</if>

<if test=' merchants_contact_mobile != null and merchants_contact_mobile != "" '>

and merchants_contact_mobile like concat('%',#{merchants_contact_mobile},'%')

</if>

<if test=' merchants_contact_name != null and merchants_contact_name != "" '>

and merchants_contact_name like concat('%',#{merchants_contact_name},'%')

</if>

<if test=' merchants_id != null and merchants_id != "" '>

and merchants_id = #{merchants_id}

</if>

) as a

left join 

(

select m.merchants_id as merchants_id,count(*) as last_week_num

from tst_member_order as m

inner join tst_house_config as n

on m.config_id = n.config_id

and n.is_delete = '0'

WHERE m.is_delete = '0' and

YEARWEEK(date_format(m.create_time,'%Y-%m-%d')) = YEARWEEK(now())-1 

group by m.merchants_id

)as b 

on a.merchants_id= b.merchants_id

left join 

(

select m.merchants_id as merchants_id,count(*) as last_day_num 

from tst_member_order as m

inner join tst_house_config as n

on m.config_id = n.config_id

and n.is_delete = '0'

WHERE m.is_delete = '0' 

and date(m.create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)

group by m.merchants_id

)as c

on a.merchants_id= c.merchants_id

left join 

(

select m.merchants_id as merchants_id,count(*) as total_num,ifNull(sum(b.total_price),0) as total_price

from tst_member_order as m

inner join tst_house_config as n

on m.config_id = m.config_id

and n.is_delete = '0'

WHERE m.is_delete = '0' 

group by m.merchants_id

)as d

on a.merchants_id= d.merchants_id

<if test=' field != "" and order != ""  '>

order by #{field} #{order}

</if>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值