gpt4 book ai didi

SQL 服务器 : join tables causes the data to duplicate on every row

转载 作者:行者123 更新时间:2023-12-02 03:39:52 24 4
gpt4 key购买 nike

我有一个返回一些冗余数据的查询。有人可以帮我更正这个查询吗?我也附上了表格关系。

SELECT
dbo.supplierOrder.sectionID,
dbo.supplierOrder.supplierID,
dbo.supplierOrder.supplierOrderNo,
dbo.supplierOrder.supplierOrderCreated,
dbo.supplierOrder.supplierOrderConfirmStatus,
dbo.supplierOrderDetails.productID,
dbo.supplierOrderDetails.orderQty,
dbo.supplierOrderReceive.invoiceno,
dbo.supplierOrderReceive.supplierInvoiceno,
dbo.supplierOrderReceive.ID,
dbo.supplierOrderReceiveDetail.qtyArrived
FROM
dbo.supplierOrder
INNER JOIN
dbo.supplierOrderDetails ON (dbo.supplierOrderDetails.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN
dbo.supplierOrderReceive ON (dbo.supplierOrderReceive.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN
dbo.supplierOrderReceiveDetail ON (dbo.supplierOrderReceiveDetail.supplierOrderReceiveID = dbo.supplierOrderReceive.ID)
WHERE
dbo.supplierOrder.ID = 1 ;

这是我得到的输出

这不是我想要的。最后一列 qtyArrived 应该只显示它​​的相关数量,但在这里你可以看到它对每个 productID 重复

enter image description here

这是数据库表和关系 enter image description here

enter image description here

最佳答案

我认为最后一张表还应该使用 ProductId 连接,而不仅仅是 OrderID :

LEFT JOIN 
dbo.supplierOrderReceiveDetail
ON
(dbo.supplierOrderReceiveDetail.supplierOrderReceiveID=dbo.supplierOrderReceive.ID)
AND
(dbo.supplierOrderReceiveDetail.ProductID =dbo.sdbo.supplierOrderDetails.ProductID)

关于SQL 服务器 : join tables causes the data to duplicate on every row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20756956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com