我正在尝试编写一个实现以下目的的MySQL查询:
基于所选的customerNumber,应显示所有
ERD如下所示:
对于每个选定的客户编号,我只需要订单,订单明细和产品表中的数据。
答案 0 :(得分:0)
您可以尝试执行此操作并更改使它生效所需的内容。
因此,每个*
必须替换为要显示的表的列的列表:
SELECT
C.customerNumber , -- the customer id
O.*, -- replace by O.column_name1, O.column_name2, ...
OD.*, -- replace by OD.column_name1, OD.column_name2, ...
P.* -- replace by P.column_name1, P.column_name2, ...
FROM customers as C
-- If you only want customer with at least ONE orders, change LEFT JOIN by INNER JOIN
LEFT JOIN orders as O on O.customerNumber = C.customerNumber
-- I suppose orderLineNumber is the same than orderNumber in table orders?
LEFT JOIN orderdetails as OD on OD.orderLineNumber = O.orderNumber
LEFT JOIN products as P on P.productCode = OD.productCode
-- The result will be order by customerNumber, of for each customerNumber by orderDate
ORDER BY C.customerNumber, O.orderDate
是您要找的东西吗?