inner join 作用
时间: 2023-10-17 08:22:48 浏览: 41
Inner join 是一种 SQL 查询操作,它的作用是将两个或多个表格中满足指定条件的行数据连接起来。Inner join 只返回符合条件的行,即两个表格中都存在的记录,因此它可以用来过滤掉不需要的数据,从而提高查询效率。
Inner join 的语法如下:
```
SELECT column_list
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
```
其中,column_list 是需要查询的列名列表,table1 和 table2 是需要连接的两个表格名,ON 子句指定了连接条件,即连接两个表格时应该使用哪个列进行匹配。
举个例子,假设我们有两个表格,一个是订单表格 order,另一个是客户表格 customer。它们的结构如下:
```
order
+----------+-----------+--------+
| order_id | customer_id | amount |
+----------+-----------+--------+
| 1 | 1001 | 200 |
| 2 | 1002 | 300 |
| 3 | 1003 | 400 |
+----------+-----------+--------+
customer
+-------------+-------------+
| customer_id | customer_name |
+-------------+-------------+
| 1001 | Alice |
| 1002 | Bob |
| 1003 | Charlie |
+-------------+-------------+
```
如果我们想要查询每个订单对应的客户名字,可以使用以下 Inner join 查询语句:
```
SELECT order.order_id, customer.customer_name
FROM order
INNER JOIN customer
ON order.customer_id = customer.customer_id;
```
这条查询语句将返回以下结果:
```
+----------+---------------+
| order_id | customer_name |
+----------+---------------+
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
+----------+---------------+
```
可以看到,Inner join 将订单表格和客户表格连接起来,只返回了两个表格中都存在的数据,即每个订单对应的客户名字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)