MySql - 通过多列获取重复项

时间:2015-03-20 08:25:10

标签: mysql

我有一个零件库存表,按PartName,WarehouseId,VendorCode(主要兴趣列)存储零件。它应该只有WarehouseId和VendorCode具有唯一的PartName条目。但是,条目是混合的,我需要为这种情况获取PartName,WarehouseId和Vendor。例如:

ABC133, Warehouse 10, VendorCode 1234
ABC133, Warehouse 10, VendorCode 1222

BBB111, Warehouse 20, VendorCode 1111
BBB111, Warehouse 20, VendorCode 2222

我已经定制了一个在此网站上找到的查询来执行此操作,但它只会带来第一个"重复"对于每个重复的PartName,我需要获取所有错误的条目:

ABC133, Warehouse 10, VendorCode 1222

BBB111, Warehouse 20, VendorCode 1111

这是我使用的查询:

SELECT i.MFGPN, i.VendorCode, i.WarehouseID FROM edi_846_inventory i
INNER JOIN (SELECT MFGPN FROM edi_846_inventory 
GROUP BY MFGPN HAVING count(MFGPN) > 1 and count(VendorCode) > 1) dup ON i.MFGPN = dup.MFGPN
where MFGPN is the PartName

由于

2 个答案:

答案 0 :(得分:0)

这是您想要的查询:

SELECT i.MFGPN, i.VendorCode, i.WarehouseID
FROM edi_846_inventory i INNER JOIN
     (SELECT MFGPN, WarehouseID
      FROM edi_846_inventory 
      GROUP BY MFGPN, WarehouseID
      HAVING count(*) > 1 
     ) dup
     ON i.MFGPN = dup.MFGPN AND i.WarehouseID = dup.WarehouseID;

换句话说,您的子查询需要按MFGPNWarehouseID进行汇总。

此外,只需将供应商连接在一起就足够了:

      SELECT MFGPN, WarehouseID, GROUP_CONCAT(VendorCode) as Vendors
      FROM edi_846_inventory 
      GROUP BY MFGPN, WarehouseID
      HAVING count(*) > 1 

答案 1 :(得分:0)

一位工作同事找到了解决方案:

选择i.MFGPN,i.WarehouseID,i.VendorCode,i.IngramSKU

来自edi_846_inventory i   i.MFGPN在哪里   (    选择 *    从    (     选择不同的i.MFGPN     来自edi_846_inventory i     i.MFGPN,i.WarehouseID分组     有计数(*)> 1    )dup   )   由i.MFGPN,i.WarehouseID

订购