如何正确的使用mysql-joins方法

  介绍

今天就跟大家聊聊有关如何正确的使用mysql-joins方法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

假设有两个表,Table_A和Table_B。这两个表中的数据如下所示:

table_a | TABLE_B   ,PK  Value ,,,,,|,,,PK 价值   - - - - -,- - - - - - - - - - -,,,,|,,,- - - - - - - - - - - - - - - -   ,1 FOX ,,,,|,,,1,小跑   ,2 COP ,,,,|,,,2,车   ,3 TAXI ,,,,|,,3,出租车   ,6 WASHINGTON ,,,|,,,6,纪念碑   ,7 DELL ,,,,|,,,7,电脑   ,5 ARIZONA ,,,|,,,8,微软   ,4 LINCOLN ,,,|,,,9,苹果   ,10 LUCENT ,,,,|,,,11日,苏格兰

加入语法:

join_table:   ,table_reference  JOIN  table_factor  [join_condition]//内连接   ,| table_reference {左| |全},(外),JOIN  table_reference  join_condition//外连接   ,| table_reference  LEFT  SEMI  JOIN  table_reference  join_condition//左半连接   ,| table_reference  CROSS  JOIN  table_reference  [join_condition], (as  of  Hive  0.10)         table_reference:   table_factor//表   ,| join_table//加入语句         table_factor:   (别名),tbl_name //表名(别名)   ,| table_subquery 别名//子查寻[别名)   ,|,table_references //带空号的table_reference         join_condition:   ,提醒表达式//开头的条件语句

1,内连接:(内连接)

如何正确的使用mysql-joins方法”> <br/> </p> <p>这是最简单,最容易理解的连接,也是最常见的连接。此查询将返回左表(表一)中具有右表(表B)中匹配记录的所有记录。此连接写成如下:</p> <pre类= SELECT  & lt; select_list>,   得到Table_A    INNER  JOIN  Table_B  B   提醒A.Key  B=,。关键 ——, Inner 加入   SELECT  A.PK  AS  A_PK, A.Value  AS  A_Value,   B.Value 才能AS  B_Value B.PK  AS  B_PK   得到Table_A    INNER  JOIN  Table_B  B   提醒A.PK =B.PK      A_PK  A_Value  B_Value  B_PK   - - - - -,- - - - - - - - - - -,- - - - - - - - - - - - - - - -   ,1 FOX  TROT ,, 1   ,2 COP  CAR ,, 2   ,3 TAXI  CAB ,, 3   ,6 WASHINGTON  MONUMENT  6   ,7 DELL  PC ,, 7      (5,行(s),影响)

2,左加入:(左连接)

如何正确的使用mysql-joins方法”> <br/> </p> <p>此查询将返回左表(表一)中的所有记录,而不管这些记录是否与右表(表B)中的任何记录匹配。它还将从正确的表中返回任何匹配的记录。此连接写成如下:</p> <pre类= SELECT  & lt; select_list>   得到Table_A    LEFT  JOIN  Table_B  B   提醒A.Key  B=,。关键 ——, Left 加入   SELECT  A.PK  AS  A_PK, A.Value  AS  A_Value,   B.Value  AS  B_Value B.PK  AS  B_PK   得到Table_A    LEFT  JOIN  Table_B  B   提醒A.PK =B.PK      A_PK  A_Value  B_Value  B_PK   - - - - -,- - - - - - - - - - -,- - - - - - - - - - - - - - - -   ,1 FOX  TROT ,, 1   ,2 COP  CAR ,, 2   ,3 TAXI  CAB ,, 3   ,4 LINCOLN  NULL 零   ,5 ARIZONA  NULL 零   ,6 WASHINGTON  MONUMENT  6   ,7 DELL  PC ,, 7   ,10 LUCENT , NULL 零      (8,行(s),影响)

3,左不包括连接:(左连接排除内连接结果)

此查询将返回左表(表一)中与右表(表B)中的任何记录都不匹配的所有记录。此连接写成如下:

如何正确的使用mysql-joins方法

SELECT  & lt; select_list>,   得到Table_A    LEFT  JOIN  Table_B  B   提醒A.Key =B.Key   WHERE  B.Key  IS 空 ——, Left  Excluding 加入   SELECT  A.PK  AS  A_PK, A.Value  AS  A_Value,   B.Value  AS  B_Value B.PK  AS  B_PK   得到Table_A    LEFT  JOIN  Table_B  B   提醒A.PK =B.PK   WHERE  B.PK  IS  NULL      A_PK  A_Value  B_Value  B_PK   - - - - -,- - - - - - - - - - -,- - - - - - - - - - - - - - - -   ,4 LINCOLN  NULL 零   ,5 ARIZONA  NULL 零   ,10 LUCENT , NULL 零   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何正确的使用mysql-joins方法