MySQL INNER JOIN子句
简介:在本教程中,您将学习如何使用MySQL INNER JOIN子句根据连接条件从多个表中选择数据。
MySQL INNER JOIN子句介绍
MySQL INNER JOIN子句将一个表中的行与其他表中的行进行匹配,并允许您查询包含两个表中列的行。
INNER JOIN子句是SELECT查询的可选部分。它出现在FROM语句之后。
在使用INNER JOIN子句之前,您必须指定以下条件:
首先,出现在FROM子句中的主表。
第二,要与主表连接的表,表出现在INNER JOIN子句中。理论上,您可以将表与许多其他表联接起来。但是,为了获得更好的性能,您应限制要加入的表的数量。
第三,连接条件或连接谓词。连接条件出现在INNER JOIN子句的ON关键字之后。连接条件是将主表中的行与其他表中的行进行匹配的规则。
INNER JOIN子句的语法如下:
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;
让我们假设我们正在连接两个表t1并t2使用INNER JOIN子句来简化上面的语法。
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition;
对于t1表中的每一行,INNER JOIN子句将其与t2表的每一行进行比较,以检查它们是否都满足连接条件。当满足连接条件时,INNER JOIN将返回一个新行,行由两个表t1和t2表组成。
注意:必须根据连接条件匹配两个表t1和t2表中的行。如果未找到匹配项,查询将返回空结果集。当使用2个以上的表时,用法也是一样的。
以下WIKI说明了INNER JOIN子句的工作原理。结果集中的行必须出现在两个表中:t1并且t2 如两个圆的交叉部分所示。
MySQL INNER JOIN 中避免出现模糊的列错误
如果连接具有相同列名的多个表,则必须使用表限定符来引用SELECT 和ON子句中的列,以避免出现模糊列错误。
例如,如果两个表t1和两个t2表都具有相同的列c ,则必须c使用表限定符作为t1.c 或t2.c 在SELECT和ON子句中引用列。
为了节省键入表限定符的时间,可以在查询中使用表别名。例如,您可以为verylongtablename表提供表的别名,t并使用t.column而不是使用它来引用其列verylongtablename.column。
MySQL INNER JOIN 实例
我们来看看示例数据库中的products和productlines表。
+--------------------+
| products |
+--------------------+
| productCode |
| productName |
| productLine |
| productScale |
| productVendor |
| productDescription |
| quantityInStock |
| buyPrice |
| MSRP |
+--------------------+
9 rows in set (0.01 sec)
+-----------------+
| productlines |
+-----------------+
| productLine |
| textDescription |
| htmlDescription |
| image |
+-----------------+
4 rows in set (0.01 sec)
在products表具有以表的productLine列关联 productlines 表productline列。products表中的productLine列称为外键列。
通常连接具有外键关系的表像 productlines和products表。
现在,如果你想得到
从products表取productCode与productName
从productlines表取textDescription
因此,需要通过productline使用INNER JOIN子句根据列匹配行来从两个表中选择数据,如下所示:
SELECT
productCode,
productName,
textDescription
FROM
products t1
INNER JOIN
productlines t2 ON t1.productline = t2.productline;
运行结果:
内容太此处省略,请亲自尝试
+-------------+---------------------------------------------+--------------------------+
| productCode | productName | left(textDescription,13) |
+-------------+---------------------------------------------+--------------------------+
| S10_1949 | 1952 Alpine Renault 1300 | Attention car ... |
| S10_4757 | 1972 Alfa Romeo GTA | Attention car ... |
| S10_4962 | 1962 LanciaA Delta 16V | Attention car ... |
| S12_1099 | 1968 Ford Mustang | Attention car ... |
| S12_1108 | 2001 Ferrari Enzo | Attention car ... |
...
由于两个表的连接列具有相同的名称 productline,因此可以使用以下语法:
SELECT
productCode,
productName,
textDescription
FROM
products
INNER JOIN
productlines USING (productline);
它返回相同的结果集,但是使用此语法,您不必使用表别名。
MySQL INNER JOIN 使用 GROUP BY子句
请参阅以下orders和orderdetails表格。
+----------------+
| orders |
+----------------+
| orderNumber |
| orderDate |
| requiredDate |
| shippedDate |
| status |
| comments |
| customerNumber |
+----------------+
7 rows in set (0.01 sec)
+-----------------+
| orderdetails |
+-----------------+
| orderNumber |
| productCode |
| quantityOrdered |
| priceEach |
| orderLineNumber |
+-----------------+
5 rows in set (0.00 sec)
可以使用INNER JOINorders和orderdetails表中获取订单号,订单状态和总销售额:
SELECT
T1.orderNumber,
status,
SUM(quantityOrdered * priceEach) total
FROM
orders AS T1
INNER JOIN
orderdetails AS T2 ON T1.orderNumber = T2.orderNumber
GROUP BY orderNumber;
运行结果:
+-------------+------------+----------+
| orderNumber | status | total |
+-------------+------------+----------+
| 10100 | Shipped | 10223.83 |
| 10101 | Shipped | 10549.01 |
| 10102 | Shipped | 5494.78 |
| 10103 | Shipped | 50218.95 |
| 10104 | Shipped | 40206.20 |
| 10105 | Shipped | 53959.21 |
...
同样,以下查询等同于上面的查询:
SELECT
orderNumber,
status,
SUM(quantityOrdered * priceEach) total
FROM
orders
INNER JOIN
orderdetails USING (orderNumber)
GROUP BY orderNumber;
MySQL INNER JOIN 使用非等于运算符
到目前为止,我们已经使用到连接字段使用等于运算符(=)来匹配行。此外,您可以使用其他运算符(如大于(>),小于()运算符来形成连接词。
以下查询使用小于(
SELECT
orderNumber,
productName,
msrp,
priceEach
FROM
products p
INNER JOIN
orderdetails o ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE
p.productcode = 'S10_1678';
运行结果:
+-------------+---------------------------------------+-------+-----------+
| orderNumber | productName | msrp | priceEach |
+-------------+---------------------------------------+-------+-----------+
| 10107 | 1969 Harley Davidson Ultimate Chopper | 95.70 | 81.35 |
| 10121 | 1969 Harley Davidson Ultimate Chopper | 95.70 | 86.13 |
| 10134 | 1969 Harley Davidson Ultimate Chopper | 95.70 | 90.92 |
| 10145 | 1969 Harley Davidson Ultimate Chopper | 95.70 | 76.56 |
| 10159 | 1969 Harley Davidson Ultimate Chopper | 95.70 | 81.35 |
...
在本教程中,您学习了如何使用MySQL INNER JOIN从多个表中查询数据。