sql查询返回太多结果和replicants

时间:2016-06-09 16:45:03

标签: mysql sql

标题说明了一切。我查询我们的数据库,即时获取复制品以及太多结果。它应该以一个单价返回一个部件ID。

    SELECT SHIPPER.SHIPPED_DATE, INVENTORY_TRANS.PART_ID, IT.QTY, CUST_ORDER_LINE.UNIT_PRICE FROM INVENTORY_TRANS 
JOIN INVENTORY_TRANS IT ON INVENTORY_TRANS.PART_ID = IT.PART_ID
        AND INVENTORY_TRANS.TRANSACTION_ID = IT.TRANSACTION_ID
JOIN CUST_ORDER_LINE ON IT.CUST_ORDER_ID = CUST_ORDER_LINE.CUST_ORDER_ID
        AND CUST_ORDER_LINE.LAST_SHIPPED_DATE = INVENTORY_TRANS.TRANSACTION_DATE
JOIN SHIPPER ON IT.CUST_ORDER_ID = SHIPPER.CUST_ORDER_ID
        AND CUST_ORDER_LINE.LAST_SHIPPED_DATE = SHIPPER.SHIPPED_DATE
        AND IT.TRANSACTION_DATE = SHIPPER.SHIPPED_DATE
WHERE SHIPPER.SHIPPED_DATE > '2016-05-01' AND SHIPPER.SHIPPED_DATE < '2016-06-01' 
ORDER BY IT.PART_ID

1 个答案:

答案 0 :(得分:0)

我认为这里不需要你自己加入INVENTORY_TRANS。在from语句中用IT或其他东西将其删除并别名:

SELECT 
 SHIPPER.SHIPPED_DATE, 
 IT.PART_ID, 
 IT.QTY, 
 CUST_ORDER_LINE.UNIT_PRICE 
FROM INVENTORY_TRANS IT
JOIN CUST_ORDER_LINE 
 ON (IT.CUST_ORDER_ID = CUST_ORDER_LINE.CUST_ORDER_ID AND CUST_ORDER_LINE.LAST_SHIPPED_DATE = IT.TRANSACTION_DATE)
JOIN SHIPPER 
 ON (IT.CUST_ORDER_ID = SHIPPER.CUST_ORDER_ID AND CUST_ORDER_LINE.LAST_SHIPPED_DATE = SHIPPER.SHIPPED_DATE AND IT.TRANSACTION_DATE = SHIPPER.SHIPPED_DATE)
WHERE SHIPPER.SHIPPED_DATE > '2016-05-01' AND SHIPPER.SHIPPED_DATE < '2016-06-01' 
ORDER BY IT.PART_ID