简体   繁体   English

MySQL在子查询中引用外部选择

[英]MySQL referencing outer select in subquery

I have two tables, shipments and customers. 我有两个表,发货和客户。 I want to print out the customers' last names with the shipments. 我想在发货中打印出客户的姓氏。 Tables are set up as: 表设置为:

shipments:  shipNumber, shipperID, destID, size, weight

shipperID and destID are foreign keys from customers table. shipperID和destID是客户表中的外键。

customers:  id, lastName, street, city, state, zip.

What I want when I query is a table like this: 查询时想要的是这样的表:

shipNumber, shipperID(customers.id), customer.lastName, destID(customers.id), customer.lastName

shipperID and destID are not the same, therefore the customers.lastName fields should only relate to the id field they come after. shipperID和destID不相同,因此customers.lastName字段应仅与它们后面的id字段相关。

My current query that doesn't work for obvious reasons: 我当前的查询由于明显的原因而无法正常工作:

SELECT shipments.shipNumber, shipments.shipperID, customers.lastName, shipments.destID, 
(SELECT customers.lastName 
FROM shipments, customers 
WHERE shipments.destID = customers.id)

FROM shipments, customers
WHERE shipments.shipperID = customers.id;

My subquery will obviously print more than one row, but I want the last name of the customer referenced by destID in relation to the shipnumber, but I'm aware that mySQL looks at queries inside out. 我的子查询显然会打印多行,但是我想要由destID引用的客户的姓氏与船号的关系,但是我知道mySQL会从内向外查找查询。

Sample Data: 样本数据:

shipments table
+------------+-----------+---------+------+--------+
| shipNumber | shipperID | destID  | size | weight |
+------------+-----------+---------+------+--------+
| S12345-0   | W-100     | C-22315 |   12 |     12 | 
| SYY89-4    | C-15572   | U-01002 |   12 |     12 | 
+------------+-----------+---------+------+--------+

customers table
+---------+------------------+
| id      | lastName         |
+---------+------------------+
| S-01210 | Home Depot       | 
| S-18537 | Honda            | 
| S-13349 | NCR              | 
| C-15572 | GM               | 
| C-00122 | Delco            | 
| W-100   | Warehouse A      | 
| U-01002 | Wright State     | 
| W-210   | Warehouse B      | 
| U-00013 | AFIT             | 
| C-22315 | Northrop Grumman | 
+---------+------------------+

What I want the query to show: 我想要查询显示的内容:

shipNumber    shipperID    lastName     destID   lastName
S12345-0      W-100        Warehouse A  C-22315  Northrop Grumman

This was data provided to me, so the lastName column being filled with data that are not last names is probably just an oversight, so just go with it. 这是提供给我的数据,因此用不是姓氏的数据填充lastName列可能只是一个疏忽,所以就随它去吧。

If i understand your question correctly, you don't need a subquery, you just need to join the two tables together. 如果我正确理解了您的问题,则不需要子查询,只需将两个表连接在一起。 the customers needs to be joined twice.. once for shipperID and once for destID to get the corresponding lastName. 客户需要加入两次。.一次获得shipperID,一次获得destID,以获得相应的lastName。 this query should do the trick.. see working FIDDLE 此查询应该做的伎俩..看到工作FIDDLE

SELECT 
    s.shipNumber, 
    s.shipperID, 
    c.lastName AS first_last_name, 
    s.destID, 
    c1.lastName AS second_last_name
FROM shipments s
JOIN customers c ON c.id = s.shipperID
JOIN customers c1 ON c1.id = s.destID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM