inner join(内连接)
返回两个表中匹配的行。
表达方式1:
select * from table1 inner join table2 on table1.column = table2.column;
表达方式2:
select * from table1, table2 where table1.column = table2.column;
left join(左连接)
返回左表中的所有行以及与右表匹配的行。
表达方式1:
select * from table1 left join table2 on table1.column = table2.column;
表达方式2:
select * from table1 left outer join table2 on table1.column = table2.column;
right join(右连接)
返回右表中的所有行以及与左表匹配的行。
表达方式1:
select * from table1 right join table2 on table1.column = table2.column;
表达方式2:
select * from table1 right outer join table2 on table1.column = table2.column;
full join(全连接)
返回左右表中的所有行。
select * from table1 full join table2 on table1.column = table2.column;
示例图形化解释join的不同类型
inner join
+----+-------+-----+-------+ | id | name | age | grade | +----+-------+-----+-------+ | 1 | john | 24 | a | | 2 | alice | 22 | b | | 3 | bob | 25 | a | +----+-------+-----+-------+ +--------+--------+ | id | city | +--------+--------+ | 1 | london | | 2 | paris | | 4 | berlin | +--------+--------+ after inner join: +----+-------+-----+-------+--------+--------+ | id | name | age | grade | id | city | +----+-------+-----+-------+--------+--------+ | 1 | john | 24 | a | 1 | london | | 2 | alice | 22 | b | 2 | paris | +----+-------+-----+-------+--------+--------+
left join
+----+-------+-----+-------+ | id | name | age | grade | +----+-------+-----+-------+ | 1 | john | 24 | a | | 2 | alice | 22 | b | | 3 | bob | 25 | a | +----+-------+-----+-------+ +--------+--------+ | id | city | +--------+--------+ | 1 | london | | 2 | paris | | 4 | berlin | +--------+--------+ after left join: +----+-------+-----+-------+--------+--------+ | id | name | age | grade | id | city | +----+-------+-----+-------+--------+--------+ | 1 | john | 24 | a | 1 | london | | 2 | alice | 22 | b | 2 | paris | | 3 | bob | 25 | a | null | null | +----+-------+-----+-------+--------+--------+
right join
+----+-------+-----+-------+ | id | name | age | grade | +----+-------+-----+-------+ | 1 | john | 24 | a | | 2 | alice | 22 | b | | 3 | bob | 25 | a | +----+-------+-----+-------+ +--------+--------+ | id | city | +--------+--------+ | 1 | london | | 2 | paris | | 4 | berlin | +--------+--------+ after right join: +----+-------+-----+-------+--------+--------+ | id | name | age | grade | id | city | +----+-------+-----+-------+--------+--------+ | 1 | john | 24 | a | 1 | london | | 2 | alice | 22 | b | 2 | paris | | null | null | null | null | 4 | berlin | +----+-------+-----+-------+--------+--------+
full join
+----+-------+-----+-------+ | id | name | age | grade | +----+-------+-----+-------+ | 1 | john | 24 | a | | 2 | alice | 22 | b | | 3 | bob | 25 | a | +----+-------+-----+-------+ +--------+--------+ | id | city | +--------+--------+ | 1 | london | | 2 | paris | | 4 | berlin | +--------+--------+ after full join: +----+-------+-----+-------+--------+--------+ | id | name | age | grade | id | city | +----+-------+-----+-------+--------+--------+ | 1 | john | 24 | a | 1 | london | | 2 | alice | 22 | b | 2 | paris | | 3 | bob | 25 | a | null | null | | null | null | null | null | 4 | berlin | +----+-------+-----+-------+--------+--------+
到此这篇关于mysql中join关键字详细使用的文章就介绍到这了,更多相关mysql json使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论