我不确定如何提出这个问题,甚至不知道如何标题,所以如果之前有人问我很抱歉。
我创建了一个连接,可以检索我需要的所有信息。排序。
它从客户表中返回客户ID而不是客户的详细信息:
SELECT o.orderTotal, o.orderID, oc.productID, oc.quantity, oc.price_per, co.customerID
FROM tblOrders o
INNER JOIN tblOrderContents oc ON o.orderID = oc.orderID
INNER JOIN tblCustomerOrders co ON o.orderID = co.orderID
我的客户表是:
**tblCustomer**
customerID
customerName etc...
如何让这个查询在同一个SQL查询中返回实际的客户信息,如名称,地址等,我甚至可以这样做吗?
答案 0 :(得分:2)
只需使用join
tblCustomer
即可
SELECT o.orderTotal, o.orderID, oc.productID, oc.quantity, oc.price_per, co.customerID, c.customerName
FROM tblOrders o
INNER JOIN tblOrderContents oc ON o.orderID = oc.orderID
INNER JOIN tblCustomerOrders co ON o.orderID = co.orderID
INNER JOIN tblCustomer c on co.customerID = c.customerID
答案 1 :(得分:2)
通过加入客户表,您可以访问任何字段。因此,只需在SELECT子句中添加所需的字段以及其他连接:
SELECT
o.orderTotal,
o.orderID,
oc.productID,
oc.quantity,
oc.price_per,
co.customerID,
cu.customerName
FROM
tblOrders o
INNER JOIN
tblOrderContents oc ON o.orderID = oc.orderID
INNER JOIN
tblCustomerOrders co ON o.orderID = co.orderID
INNER JOIN
tblCustomer cu ON cu.customerID = co.customerID
答案 2 :(得分:0)
如果客户的信息在tblCustomer中,您可以将此查询中的结果与此表联系起来。
select I.*
from
(SELECT o.orderTotal, o.orderID, oc.productID, oc.quantity, oc.price_per, co.customerID
FROM tblOrders o
INNER JOIN tblOrderContents oc ON o.orderID = oc.orderID
INNER JOIN tblCustomerOrders co ON o.orderID = co.orderID) as O left join tblCustomer I on O.customerId=I.customerId