Mysql 中内连接、左连接和右连接的区别在于:内连接只返回同时在两个表中匹配的行,而左连接返回左表所有行,包含匹配右表行,右连接返回右表所有行,包含匹配左表行。内连接语法:selec&
Mysql 中内连接、左连接和右连接的区别在于:内连接只返回同时在两个表中匹配的行,而左连接返回左表所有行,包含匹配右表行,右连接返回右表所有行,包含匹配左表行。内连接语法:select * from table1 inner join table2 on table1.column1 = table2.column2;左连接语法:select * from table1 left join table2 on table1.column1 = table2.column2;右连接语法:sele
MySQL 中内连接、左连接和右连接的区别
内连接 (INNER JOIN)
左连接 (LEFT JOIN)
右连接 (RIGHT JOIN)
用法
语法
内连接:
<code class="sql">SELECT *
FROM table1
INNER JOIN table2
ON table1.column1 = table2.column2;</code>
左连接:
<code class="sql">SELECT *
FROM table1
LEFT JOIN table2
ON table1.column1 = table2.column2;</code>
右连接:
<code class="sql">SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column1 = table2.column2;</code>
例子
假设我们有以下两个表:
<code>Table1:
| id | name |
|---|---|
| 1 | John |
| 2 | Mary |
| 3 | Bob |
Table2:
| id | address |
|---|---|
| 1 | 123 Main St |
| 2 | 456 Elm St |
| 4 | 789 Oak St |</code>
内连接:
<code class="sql">SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.id = Table2.id;</code>
结果:
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
左连接:
<code class="sql">SELECT *
FROM Table1
LEFT JOIN Table2
ON Table1.id = Table2.id;</code>
结果:
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
3 | Bob | NULL |
右连接:
<code class="sql">SELECT *
FROM Table1
RIGHT JOIN Table2
ON Table1.id = Table2.id;</code>
结果:
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
4 | NULL | 789 Oak St |
--结束END--
本文标题: mysql中内连接,左连接和右连接的区别
本文链接: https://www.lsjlt.com/news/610590.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0