JOIN

JOIN 用于组合两个或多个表中基于相关列的行。 它允许你从多个表中提取数据,并将其呈现为单个结果集。

JOIN 语法有以下类型:

  • INNER JOIN:仅返回两个表中具有匹配值的行。
  • LEFT JOIN:返回左表中的所有行和右表中的匹配行。
  • RIGHT JOIN:返回右表中的所有行和左表中的匹配行。
  • FULL OUTER JOIN:返回两个表中的所有行。

示例

下面是使用 JOIN 子句的一些示例:

sql

  1. -- Select all rows from the system_metrics table and idc_info table where the idc_id matches
  2. SELECT a.*
  3. FROM system_metrics a
  4. JOIN idc_info b
  5. ON a.idc = b.idc_id;
  6. -- Select all rows from the idc_info table and system_metrics table where the idc_id matches, and include null values for idc_info without any matching system_metrics
  7. SELECT a.*
  8. FROM idc_info a
  9. LEFT JOIN system_metrics b
  10. ON a.idc_id = b.idc;
  11. -- Select all rows from the system_metrics table and idc_info table where the idc_id matches, and include null values for idc_info without any matching system_metrics
  12. SELECT b.*
  13. FROM system_metrics a
  14. RIGHT JOIN idc_info b
  15. ON a.idc = b.idc_id;