TOP

SQL INNER JOIN

SQL INNER JOIN 描述

INNER 关键字 JOIN 选择在两个表中都有匹配值的记录。


SQL INNER JOIN 语法

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name

SQL INNER JOIN

演示数据库

以下是表“Orders”(“订单”)中的示例:

ProductIDOrderIDCustomerIDEmployeeIDOrderDateShipperID
1102489051996-07-043
2102498161996-07-051
3102503441996-07-082

并从表“Customers”(“客户”)中进行选择:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico

SQL INNER JOIN 示例

以下 SQL 语句选择所有带有客户信息的订单:

Run SQLSELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
如果列之间存在匹配,则 INNER 关键字 JOIN 将从两个表中选择所有行。 如果表“Orders”(“订单”)中存在与“Customers”(“客户”)中不匹配的记录,则这些订单将不会显示!

SQL INNER JOIN 连接三个表

以下 SQL 语句选择包含客户和发货人信息的所有订单:

Run SQLSELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName 
FROM ((Orders INNER JOIN Customers 
       ON Orders.CustomerID = Customers.CustomerID) 
       
      INNER JOIN Shippers 
      ON Orders.ShipperID = Shippers.ShipperID)