试图比较2个表与多个联接

时间:2015-02-05 21:49:10

标签: mysql

我正在尝试比较2个表,但似乎无法让它工作。这可能是非常基本的东西。这是我的代码:

Select bin, partno, count(*) from 
(SELECT parts.partno, location.bin
    FROM inventory
    INNER JOIN location
        ON inventory.locid = location.locid
    INNER JOIN parts
        ON inventory.partid = parts.partid
WHERE  loc = 'PnP'
UNION             
select partno, bin from dlyfeedercontents) t
Group By t.bin, t.partno
HAVING COUNT(*) = 1
ORDER BY bin

结果如下:

08-01   3052-93-7100-0C6    1
08-01   3052-93-7100-0C6    1
08-01   Test2               1
08-02   3052-90-7100-063    1
08-02   3052-90-7100-063    1

我似乎无法让Group或Count承认存在重复(例如,前两行)。 UNION的结果是我的预期。

2 个答案:

答案 0 :(得分:0)

我不太确定你要做什么,但我强烈怀疑问题是使用union而不是union allunion删除重复项。这个版本是否符合您的要求?

select bin, partno, count(*) 
from ((SELECT parts.partno, location.bin
       FROM inventory INNER JOIN
            location
            ON inventory.locid = location.locid INNER JOIN
            parts
            ON inventory.partid = parts.partid
       WHERE loc = 'PnP'
      ) union all
      (select partno, bin
       from dlyfeedercontents
      )
     ) t
Group By t.bin, t.partno
HAVING COUNT(*) = 1
ORDER BY bin;

答案 1 :(得分:0)

问题是某些数据(/ r)中的非打印字符!感谢所有帮助过的人!