推特 阿里云技术文档正文

JOIN_SELECT_SQL手册_分析型数据库MySQL版

admin 阿里云技术文档 2020-02-11 195 0
阿里云服务器优惠

JOIN

语法

  1. join_table:
  2. table_reference [INNER] JOIN table_factor [join_condition]
  3. | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition
  4. | table_reference CROSS JOIN table_reference [join_condition])
  5. table_reference:
  6. table_factor
  7. | join_table
  8. table_factor:
  9. tbl_name [alias]
  10. | table_subquery alias
  11. | ( table_references )
  12. join_condition:
  13. ON expression

示例

以下查询由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;
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----