全部产品
云市场

JOIN

更新时间:2018-12-02 19:18:21

以下查询是FROM子句中的两个子查询的内部联接,此查询查找不同类别活动 (音乐会和演出) 的已售门票数和未售门票数。

  1. select catgroup1, sold, unsold
  2. from
  3. (select catgroup, sum(qtysold) as sold
  4. from category c, event e, sales s
  5. where c.catid = e.catid and e.eventid = s.eventid
  6. group by catgroup) as a(catgroup1, sold)
  7. join
  8. (select catgroup, sum(numtickets)-sum(qtysold) as unsold
  9. from category c, event e, sales s, listing l
  10. where c.catid = e.catid and e.eventid = s.eventid
  11. and s.listid = l.listid
  12. group by catgroup) as b(catgroup2, unsold)
  13. on a.catgroup1 = b.catgroup2
  14. order by 1;