我一直堅持在MySQL查詢。該表是:查詢 - 分鐘(成本),並顯示供應商的成本
CREATE TABLE items_costs (
UPC varchar(15) NOT NULL,
SupplierID int(11) NOT NULL,
Current_as_of_Date datetime DEFAULT NULL,
Cost1 float DEFAULT NULL,
Cost2 float DEFAULT NULL,
Cost3 float DEFAULT NULL,
MSRP float DEFAULT NULL,
MAP float DEFAULT NULL,
Unique_Entry_Id datetime DEFAULT NULL,
PRIMARY KEY (UPC,SupplierID),
KEY SupplierID (SupplierID),
CONSTRAINT items_costs_ibfk_1 FOREIGN KEY (UPC) REFERENCES items (UPC),
CONSTRAINT items_costs_ibfk_2 FOREIGN KEY (SupplierID) REFERENCES suppliers (SupplierID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8`
我得到的最低COST1,Cost2,COST3的每個項目:
MIN(LEAST(IFNULL(ic.Cost3, ic.Cost1), IFNULL(ic.Cost2, ic.Cost1), ic.Cost1)) AS Cost
不過,現在我需要獲得與特定成本相關的供應商ID
我已經試過:
SELECT
ic.UPC,
ic.SupplierID,
ic.Current_as_of_Date,
ic.Cost1 AS Cost,
ic.MSRP,
ic.MAP,
s.SupplierName,
s.Map_YN AS Supplier_MAP
FROM
items_costs AS ic
JOIN
suppliers AS s ON s.supplierId = ic.SupplierID
WHERE
(ic.upc , ic.Cost1) IN (SELECT
ic_min.UPC,
MIN(LEAST(IFNULL(ic_min.Cost3, ic_min.Cost1),
IFNULL(ic_min.Cost2, ic_min.Cost1),
ic_min.Cost1)) AS Cost
FROM
items_costs ic_min
GROUP BY ic_min.UPC))
...這給通過UPC最低的成本,在其他線程建議。但是,仍然有重複的UPC,這應該是獨一無二的。
我在這裏做錯了什麼?
非常感謝!