`
Tristan_S
  • 浏览: 362069 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

左连接

    博客分类:
  • jj
 
阅读更多
如果用内连接 第二张表没数据的话, 第一张表的内容就无法显示。

统计优惠券使用个数, 第一张表是总数,第二张表是已用数量。

  select a.coupon_id, a.count, b.used, a.mc_code
    from (select count(1) as count, c.coupon_id, c.mc_code
            from T_CS_MEMBER_COUPON_LIST c
           where c.mc_code = '30862'
           group by c.coupon_id, c.mc_code) a
    left join (select count(1) as used, c.coupon_id, c.mc_code
                 from T_CS_MEMBER_COUPON_LIST c
                where c.mc_code = '30862'
                  and c.use_flag = 1
                group by c.coupon_id, c.mc_code) b
      on a.coupon_id = b.coupon_id


where 要放在left join on 后面

select r.rule_name, c.coupon_id, c.partner_id, c.Mc_Code, c.use_flag, c.actual_discount, c.date_succeed, c.create_on
  from T_CS_MEMBER_COUPON_LIST c
  left join t_Cs_Coupon_Rule r
    on r.id = c.coupon_id
 where c.mc_code = '30861'
   and c.coupon_id = 2000
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics