INNER JOIN命令返回两个表中具有匹配值的行。
以下SQL选择带有客户信息的所有订单:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
注意:只要各列之间有匹配项,INNER JOIN关键字就会从两个表中选择所有行。如果“Orders”表中有与“Customers”中不匹配的记录,则这些订单将不会显示!
以下SQL语句选择所有包含客户(CustomerID)和托运人(ShipperID)信息的订单:
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName FROM ((Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);