当前位置:Gxlcms > 数据库问题 > Mysql 连接(left join, right join, inner join ,full join)

Mysql 连接(left join, right join, inner join ,full join)

时间:2021-07-01 10:21:17 帮助过:17人阅读

 在mysql的数据查询过程中,我们不仅仅会遇到单表查询的情况,特别在软件开发的过程中我们很多情况下是关联其他表查询相关的数据。

    在此我们将学习MySQL 的 JOIN 在两个或多个表中查询数据。可以在SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。

JOIN 按照功能大致分为如下三类:

  • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

  • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。

  • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

相关练习的表class1 和class2 数据

mysql> select  *  from  class1;
+-----+--------+--------+
| id  | name   | class  |
+-----+--------+--------+
|  17 | linux  | 66     |
|  18 | python | 23     |
|  19 | linux  | 66     |
|  20 | python | 23     |
|  21 | linux  | 66     |
|  22 | python | 23     |
|  23 | linux  | 66     |
|  24 | python | 23     |
|  25 | linux  | 66     |
|  26 | python | 23     |
|  27 | linux  | 66     |
|  28 | python | 23     |
|  29 | linux  | 66     |
|  30 | python | 23     |
|  31 | linux  | 66     |
|  32 | python | 23     |
|  33 | linux  | 66     |
|  34 | python | 23     |
|  35 | linux  | 66     |
|  36 | python | 23     |
|  37 | linux  | 66     |
|  38 | python | 23     |
|  39 | linux  | 66     |
|  40 | python | 23     |
|  41 | linux  | 66     |
|  42 | python | 23     |
|  43 | linux  | 66     |
|  44 | python | 23     |
|  45 | linux  | 66     |
|  46 | python | 23     |
|  47 | linux  | 66     |
|  48 | python | 23     |
|  49 | linux  | 66     |
|  50 | python | 23     |
|  51 | linux  | 66     |
|  52 | python | 23     |
|  53 | linux  | 66     |
|  54 | python | 23     |
|  55 | linux  | 66     |
|  56 | python | 23     |
|  57 | linux  | 66     |
|  58 | python | 23     |
|  59 | linux  | 66     |
|  60 | python | 23     |
|  61 | linux  | 66     |
|  62 | python | 23     |
|  63 | linux  | 66     |
|  64 | python | 23     |
|  65 | linux  | 66     |
|  66 | python | 23     |
|  67 | linux  | 66     |
|  68 | python | 23     |
|  69 | linux  | 66     |
|  70 | python | 23     |
|  71 | linux  | 66     |
|  72 | python | 23     |
|  73 | linux  | 66     |
|  74 | python | 23     |
|  75 | linux  | 66     |
|  76 | python | 23     |
|  77 | linux  | 66     |
|  78 | python | 23     |
|  79 | linux  | 66     |
|  80 | python | 23     |
|  81 | linux  | 66     |
|  82 | python | 23     |
|  83 | linux  | 66     |
|  84 | python | 23     |
|  85 | linux  | 66     |
|  86 | python | 23     |
|  87 | linux  | 66     |
|  88 | python | 23     |
|  89 | linux  | 66     |
|  90 | python | 23     |
|  91 | linux  | 66     |
|  92 | python | 23     |
|  93 | linux  | 66     |
|  94 | python | 23     |
|  95 | Tom    | 1year  |
|  96 | Jack   | 2year  |
|  97 | Yaheng | winner |
|  98 | Tom    | 1year  |
|  99 | Jack   | 2year  |
| 100 | Yaheng | winner |
| 101 | Tom    | 1year  |
| 102 | Jack   | 2year  |
| 103 | Yaheng | winner |
| 104 | Tom    | 1year  |
| 105 | Jack   | 2year  |
| 106 | Yaheng | winner |
| 107 | Tom    | 1year  |
| 108 | Jack   | 2year  |
| 109 | Yaheng | winner |
| 110 | Tom    | 1year  |
| 111 | Jack   | 2year  |
| 112 | Yaheng | winner |
| 113 | Tom    | 1year  |
| 114 | Jack   | 2year  |
| 115 | Yaheng | winner |
| 116 | Tom    | 1year  |
| 117 | Jack   | 2year  |
| 118 | Yaheng | winner |
| 119 | Tom    | 1year  |

练习表class2

mysql> select  *  from  class2;
+-----+--------+---------+
| id  | name   | class   |
+-----+--------+---------+
|  17 | linux  | 66      |
|  18 | python | 23      |
|  19 | linux  | 66      |
|  20 | python | 23      |
|  21 | linux  | 66      |
|  22 | python | 23      |
|  23 | linux  | 66      |
|  24 | python | 23      |
|  25 | linux  | 66      |
|  26 | python | 23      |
|  27 | linux  | 66      |
|  28 | python | 23      |
|  29 | linux  | 66      |
|  30 | python | 23      |
|  31 | linux  | 66      |
|  32 | python | 23      |
|  33 | linux  | 66      |
|  34 | python | 23      |
|  35 | linux  | 66      |
|  36 | python | 23      |
|  37 | linux  | 66      |
|  38 | python | 23      |
|  39 | linux  | 66      |
|  40 | python | 23      |
|  41 | linux  | 66      |
|  42 | python | 23      |
|  43 | linux  | 66      |
|  44 | python | 23      |
|  45 | linux  | 66      |
|  46 | python | 23      |
|  47 | linux  | 66      |
|  48 | python | 23      |
|  49 | linux  | 66      |
|  50 | python | 23      |
|  51 | linux  | 66      |
|  52 | python | 23      |
|  53 | linux  | 66      |
|  54 | python | 23      |
|  55 | linux  | 66      |
|  56 | python | 23      |
|  57 | linux  | 66      |
|  58 | python | 23      |
|  59 | linux  | 66      |
|  60 | python | 23      |
|  61 | linux  | 66      |
|  62 | python | 23      |
|  63 | linux  | 66      |
|  64 | python | 23      |
|  65 | linux  | 66      |
|  66 | python | 23      |
|  67 | linux  | 66      |
|  68 | python | 23      |
|  69 | linux  | 66      |
|  70 | python | 23      |
|  71 | linux  | 66      |
|  72 | python | 23      |
|  73 | linux  | 66      |
|  74 | python | 23      |
|  75 | linux  | 66      |
|  76 | python | 23      |
|  77 | linux  | 66      |
|  78 | python | 23      |
|  79 | linux  | 66      |
|  80 | python | 23      |
|  81 | linux  | 66      |
|  82 | python | 23      |
|  83 | linux  | 66      |
|  84 | python | 23      |
|  85 | linux  | 66      |
|  86 | python | 23      |
|  87 | linux  | 66      |
|  88 | python | 23      |
|  89 | linux  | 66      |
|  90 | python | 23      |
|  91 | linux  | 66      |
|  92 | python | 23      |
|  93 | linux  | 66      |
|  94 | python | 23      |
|  95 | Tom    | 1year   |
|  96 | Jack   | 2year   |
|  97 | Yaheng | winner  |
|  98 | Tom    | 1year   |
|  99 | Jack   | 2year   |
| 100 | Yaheng | winner  |
| 101 | Tom    | 1year   |
| 102 | Jack   | 2year   |
| 103 | Yaheng | winner  |
| 104 | Tom    | 1year   |
| 105 | Jack   | 2year   |
| 106 | Yaheng | winner  |
| 107 | Tom    | 1year   |
| 108 | Jack   | 2year   |
| 109 | Yaheng | winner  |
| 110 | Tom    | 1year   |
| 111 | Jack   | 2year   |
| 112 | Yaheng | winner  |
| 113 | Tom    | 1year   |
| 114 | Jack   | 2year   |
| 115 | Yaheng | winner  |
| 116 | Tom    | 1year   |
| 117 | Jack   | 2year   |
| 118 | Yaheng | winner  |
| 119 | Tom    | 1year   |
| 120 | Jack   | 2year   |
| 121 | Yaheng | winner  |
| 122 | Tom    | 1year   |
| 123 | Jack   | 2year   |
| 124 | Yaheng | winner  |
| 125 | Tom    | 1year   |
| 126 | Jack   | 2year   |
| 127 | Yaheng | winner  |
| 128 | Tom    | 1year   |
| 129 | Jack   | 2year   |
| 130 | Yaheng | winner  |
| 131 | linux  | 66      |
| 132 | python | 23      |
| 133 | linux  | 66      |
| 134 | python | 23      |
| 135 | linux  | 66      |
| 136 | python | 23      |
| 137 | linux  | 66      |
| 138 | python | 23      |
| 139 | linux  | 66      |
| 140 | python | 23      |
| 141 | linux  | 66      |
| 142 | python | 23      |
| 143 | linux  | 66      |
| 144 | python | 23      |
| 145 | linux  | 66      |
| 146 | python | 23      |
| 147 | linux  | 66      |
| 148 | python | 23      |
| 149 | linux  | 66      |
| 150 | python | 23      |
| 151 | linux  | 66      |
| 152 | python | 23      |
| 153 | linux  | 66      |
| 154 | python | 23      |
| 155 | linux  | 66      |
| 156 | python | 23      |
| 157 | linux  | 66      |
| 158 | python | 23      |
| 159 | linux  | 66      |

 现在我们使用inner join 查询

 select  * from class1 inner join class2 on class1.id=class2.id; #查询两个表中ID一样的
 或者 select   * from  class1,class2 where class1.id=class2.id

执行结果:

 select   * from  class1,class2 where class1.id=class2.id;
+-----+--------+--------+-----+--------+--------+
| id  | name   | class  | id  | name   | class  |
+-----+--------+--------+-----+--------+--------+
|  17 | linux  | 66     |  17 | linux  | 66     |
|  18 | python | 23     |  18 | python | 23     |
|  19 | linux  | 66     |  19 | linux  | 66     |
|  20 | python | 23     |  20 | python | 23     |
|  21 | linux  | 66     |  21 | linux  | 66     |
|  22 | python | 23     |  22 | python | 23     |
|  23 | linux  | 66     |  23 | linux  | 66     |

mysql> select * from class1  inner join class2 on class1.id=class2.id;
+-----+--------+--------+-----+--------+--------+
| id  | name   | class  | id  | name   | class  |
+-----+--------+--------+-----+--------+--------+
|  17 | linux  | 66     |  17 | linux  | 66     |
|  18 | python | 23     |  18 | python | 23     |
|  19 | linux  | 66     |  19 | linux  | 66     |
|  20 | python | 23     |  20 | python | 23     |
|  21 | linux  | 66     |  21 | linux  | 66     |
|  22 | python | 23     |  22 | python | 23     |
|  23 | linux  | 66     |  23 | linux  | 66     |
 由于表中的数据量大,所以只截取了一部分。由此看出inner join 是根据两张表或者
 多张表满足一定关系求相关的交集。

left join 实践  left join 根据左边的表查询关系和右边的关系一样的,即使右边的表没有数据

mysql> select * from class1  left  join class2 on class1.id=class2.id;
+-----+--------+--------+------+--------+-------+
| id  | name   | class  | id   | name   | class |
+-----+--------+--------+------+--------+-------+
|  17 | linux  | 66     |   17 | linux  | 66    |
|  18 | python | 23     |   18 | python | 23    |
|  19 | linux  | 66     |   19 | linux  | 66    |
|  20 | python | 23     |   20 | python | 23    |
|  21 | linux  | 66     |   21 | linux  | 66    |
|  22 | python | 23     |   22 | python | 23    |
|  23 | linux  | 66     |   23 | linux  | 66    |
|  24 | python | 23     |   24 | python | 23    |
|  25 | linux  | 66     |   25 | linux  | 66    |
|  26 | python | 23     |   26 | python | 23    |
|  27 | linux  | 66     |   27 | linux  | 66    |
|  28 | python | 23     |   28 | python | 23    |
|  29 | linux  | 66     |   29 | linux  | 66    |
|  30 | python | 23     |   30 | python | 23    |
|  31 | linux  | 66     |   31 |        |       |
|  32 | python | 23     |   32 |        |       |
|  33 | linux  | 66     |   33 |        |       |
|  34 | python | 23     |   34 |        |       |
|  35 | linux  | 66     |   35 |        |       |
|  36 | python | 23     |   36 |        |       |
|  37 | linux  | 66     |   37 |        |       |

根据结果可以发现 右边的部分表是没有数据的但是还是可以匹配到,所以left join是根据左边的表
匹配相关内容。

right join   与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

  select * from class1  right  join class2 on class1.id=class2.id;
 
  127 | Yaheng | winner | 127 |        |       |
|  128 | Tom    | 1year  | 128 |        |       |
|  129 | Jack   | 2year  | 129 |        |       |
|  130 | Yaheng | winner | 130 |        |       |
| NULL | NULL   | NULL   | 131 |        |       |
| NULL | NULL   | NULL   | 132 |        |       |
| NULL | NULL   | NULL   | 133 |        |       |
| NULL | NULL   | NULL   | 134 |        |       |
| NULL | NULL   | NULL   | 135 |        |       |
| NULL | NULL   | NULL   | 136 |        |       |
| NULL | NULL   | NULL   | 137 |        |       |
| NULL | NULL   | NULL   | 138 |        |       |
| NULL | NULL   | NULL   | 139 |        |       |
| NULL | NULL   | NULL   | 140 |        |       |
| NULL | NULL   | NULL   | 141 |        |       |
| NULL | NULL   | NULL   | 142 |        |       |
| NULL | NULL   | NULL   | 143 |        |       |
| NULL | NULL   | NULL   | 144 |        |       |
| NULL | NULL   | NULL   | 145 |        |       |
| NULL | NULL   | NULL   | 146 |        |       |
| NULL | NULL   | NULL   | 147 |        |       |
| NULL | NULL   | NULL   | 148 |        |       |
| NULL | NULL   | NULL   | 149 |        |       |
| NULL | NULL   | NULL   | 150 |        |       |
| NULL | NULL   | NULL   | 151 |        |       |
| NULL | NULL   | NULL   | 152 |        |       |
| NULL | NULL   | NULL   | 153 |        |       |
| NULL | NULL   | NULL   | 154 |        |       |
| NULL | NULL   | NULL   | 155 |        |       |
| NULL | NULL   | NULL   | 156 |        |       |
| NULL | NULL   | NULL   | 157 |        |       |
| NULL | NULL   | NULL   | 158 |        |       |
| NULL | NULL   | NULL   | 159 |        |       |
| NULL | NULL   | NULL   | 160 |        |       |
| NULL | NULL   | NULL   | 161 |        |       |
| NULL | NULL   | NULL   | 162 |        |       |
| NULL | NULL   | NULL   | 163 |        |       |
| NULL | NULL   | NULL   | 164 |        |       |
| NULL | NULL   | NULL   | 165 |        |       |
| NULL | NULL   | NULL   | 166 |        |       |
| NULL | NULL   | NULL   | 167 |        |       |
| NULL | NULL   | NULL   | 168 |        |       |
| NULL | NULL   | NULL   | 169 |        |       |
| NULL | NULL   | NULL   | 170 |        |       |
| NULL | NULL   | NULL   | 171 |        |       |
| NULL | NULL   | NULL   | 172 |        |       |
| NULL | NULL   | NULL   | 173 |        |       |
| NULL | NULL   | NULL   | 174 |        |       |
| NULL | NULL   | NULL   | 175 |        |       |
| NULL | NULL   | NULL   | 176 |        |       |
| NULL | NULL   | NULL   | 177 |        |       |
| NULL | NULL   | NULL   | 178 |        |       |
| NULL | NULL   | NULL   | 179 |        |       |
| NULL | NULL   | NULL   | 180 |        |       |
| NULL | NULL   | NULL   | 181 |        |       |
| NULL | NULL   | NULL   | 182 |        |       |
| NULL | NULL   | NULL   | 183 |        |       |
| NULL | NULL   | NULL   | 184 |        |       |
| NULL | NULL   | NULL   | 185 |        |       |
| NULL | NULL   | NULL   | 186 |        |       |
| NULL | NULL   | NULL   | 187 |        |       |
| NULL | NULL   | NULL   | 188 |        |       |
| NULL | NULL   | NULL   | 189 |        |       |
| NULL | NULL   | NULL   | 190 |        |       |
| NULL | NULL   | NULL   | 191 |        |       |
| NULL | NULL   | NULL   | 192 |        |       |
| NULL | NULL   | NULL   | 193 |        |       |
| NULL | NULL   | NULL   | 194 |        |       |
| NULL | NULL   | NULL   | 195 |        |       |
| NULL | NULL   | NULL   | 196 |        |       |
| NULL | NULL   | NULL   | 197 |        |       |
| NULL | NULL   | NULL   | 198 |        |       |
| NULL | NULL   | NULL   | 199 |        |       |
| NULL | NULL   | NULL   | 200 |        |       |
| NULL | NULL   | NULL   | 201 |        |       |
| NULL | NULL   | NULL   | 202 |        |       |
| NULL | NULL   | NULL   | 203 |        |       |
| NULL | NULL   | NULL   | 204 |        |       |
| NULL | NULL   | NULL   | 205 |        |       |
| NULL | NULL   | NULL   | 206 |        |       |
| NULL | NULL   | NULL   | 207 |        |       |
| NULL | NULL   | NULL   | 208 |        |       |
| NULL | NULL   | NULL   | 209 |        |       |
| NULL | NULL   | NULL   | 210 |        |       |
| NULL | NULL   | NULL   | 211 |        |       |
| NULL | NULL   | NULL   | 212 |        |       |
| NULL | NULL   | NULL   | 213 |        |       |
| NULL | NULL   | NULL   | 214 |        |       |
| NULL | NULL   | NULL   | 215 |        |       |
| NULL | NULL   | NULL   | 216 |        |       |
| NULL | NULL   | NULL   | 217 |        |       |
| NULL | NULL   | NULL   | 218 |        |       |
| NULL | NULL   | NULL   | 219 |        |       |
| NULL | NULL   | NULL   | 220 |        |       |
| NULL | NULL   | NULL   | 221 |        |       |
| NULL | NULL   | NULL   | 222 |        |       |
| NULL | NULL   | NULL   | 223 |        |       |
| NULL | NULL   | NULL   | 224 |        |       |
| NULL | NULL   | NULL   | 225 |        |       |
| NULL | NULL   | NULL   | 226 |        |       |
| NULL | NULL   | NULL   | 227 |        |       |
| NULL | NULL   | NULL   | 228 |        |       |
| NULL | NULL   | NULL   | 229 |        |       |
| NULL | NULL   | NULL   | 230 |        |       |
| NULL | NULL   | NULL   | 231 |        |       |
| NULL | NULL   | NULL   | 232 |        |       |
| NULL | NULL   | NULL   | 233 |        |       |
| NULL | NULL   | NULL   | 234 |        |       |
| NULL | NULL   | NULL   | 235 |        |       |
| NULL | NULL   | NULL   | 236 |        |       |
| NULL | NULL   | NULL   | 237 |        |       |
| NULL | NULL   | NULL   | 238 |        |       |
| NULL | NULL   | NULL   | 239 |        |       |
| NULL | NULL   | NULL   | 240 |        |       |
| NULL | NULL   | NULL   | 241 |        |       |
| NULL | NULL   | NULL   | 242 |        |       |
| NULL | NULL   | NULL   | 243 |        |       |
| NULL | NULL   | NULL   | 244 |        |       |
| NULL | NULL   | NULL   | 245 |        |       |
| NULL | NULL   | NULL   | 246 |        |       |
| NULL | NULL   | NULL   | 247 |        |       |
| NULL | NULL   | NULL   | 248 |        |       |
| NULL | NULL   | NULL   | 249 |        |       |
| NULL | NULL   | NULL   | 250 |        |       |
| NULL | NULL   | NULL   | 251 |

还有一种是full join,mysql不支持,但是我们可以将左连接和右连接结合,这样既可达到此类的效果

示例:

select * from class1  left  join class2 on class1.id=class2.id  UNION select * from class1
  right  join class2 on class1.id=class2.id;
id   | name   | class  | id   | name   | class |
+------+--------+--------+------+--------+-------+
|   17 | linux  | 66     |   17 | linux  | 66    |
|   18 | python | 23     |   18 | python | 23    |
|   19 | linux  | 66     |   19 | linux  | 66    |
|   20 | python | 23     |   20 | python | 23    |
|   21 | linux  | 66     |   21 | linux  | 66    |
|   22 | python | 23     |   22 | python | 23    |
|   23 | linux  | 66     |   23 | linux  | 66    |
|   24 | python | 23     |   24 | python | 23    |
|   25 | linux  | 66     |   25 | linux  | 66    |
|   26 | python | 23     |   26 | python | 23    |
|   27 | linux  | 66     |   27 | linux  | 66    |
|   28 | python | 23     |   28 | python | 23    |
|   29 | linux  | 66     |   29 | linux  | 66    |
|   30 | python | 23     |   30 | python | 23    |
|   31 | linux  | 66     |   31 |        |       |
|   32 | python | 23     |   32 |        |       |
|   33 | linux  | 66     |   33 |        |       |
|   34 | python | 23     |   34 |        |       |
|   35 | linux  | 66     |   35 |        |       |
|   36 | python | 23     |   36 |        |       |
|   37 | linux  | 66     |   37 |        |       |
|   38 | python | 23     |   38 |        |       |
|   39 | linux  | 66     |   39 |        |       |
|   40 | python | 23     |   40 |        |       |
|   41 | linux  | 66     |   41 |        |       |
|   42 | python | 23     |   42 |        |       |
|   43 | linux  | 66     |   43 |        |       |
|   44 | python | 23     |   44 |        |       |
|   45 | linux  | 66     |   45 |        |       |
|   46 | python | 23     |   46 |        |       |
|   47 | linux  | 66     |   47 |        |       |
|   48 | python | 23     |   48 |        |       |
|   49 | linux  | 66     |   49 |        |       |
|   50 | python | 23     |   50 |        |       |
|   51 | linux  | 66     |   51 |        |       |
|   52 | python | 23     |   52 |        |       |
|   53 | linux  | 66     |   53 |        |       |
|   54 | python | 23     |   54 |        |       |
|   55 | linux  | 66     |   55 |        |       |
|   56 | python | 23     |   56 |        |       |
|   57 | linux  | 66     |   57 |        |       |
|   58 | python | 23     |   58 |        |       |
|   59 | linux  | 66     |   59 |        |       |
|   60 | python | 23     |   60 |        |       |
|   61 | linux  | 66     |   61 |        |       |
|   62 | python | 23     |   62 |        |       |
|   63 | linux  | 66     |   63 |        |       |
|   64 | python | 23     |   64 |        |       |
|   65 | linux  | 66     |   65 |        |       |
|   66 | python | 23     |   66 |        |       |
|   67 | linux  | 66     |   67 |        |       |
|   68 | python | 23     |   68 |        |       |
|   69 | linux  | 66     |   69 |        |       |
|   70 | python | 23     |   70 |        |       |
|   71 | linux  | 66     |   71 |        |       |
|   72 | python | 23     |   72 |        |       |
|   73 | linux  | 66     |   73 |        |       |
|   74 | python | 23     |   74 |        |       |
|   75 | linux  | 66     |   75 |        |       |
|   76 | python | 23     |   76 |                     

人气教程排行