mysql查找匹配键不匹配的值

时间:2012-11-07 19:09:10

标签: mysql

我正在使用一个有一些重复数据的数据库,并且我正在尝试编写一个可以提取问题数据的查询。该表看起来像:

lpID, pID, pName, etc...

其中lpID是此表的唯一ID,pID是整个db的共享ID。我有这个来拉出匹配的pID值(16k):

SELECT lp.pID, group_concat(lp.lpID) as lpIDs, group_concat(lp.pName) as names
FROM lp
INNER JOIN (
  SELECT pID
  FROM lp
  GROUP BY pID
  HAVING count(pID) > 1
) dup ON lp.pID = dup.pID
group by lp.pID

哪个有效,但现在我试图扩展它,使它只拉出pName值不匹配的938。我尝试的任何东西似乎都没有用。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

你试过吗

SELECT lp.pID, group_concat(lp.lpID) as lpIDs, group_concat(lp.pName) as names
FROM lp
INNER JOIN (
  SELECT pID, COUNT(distinct pName) AS Names
  FROM lp
  GROUP BY pID
  HAVING count(pID) > 1 AND Names > 1
) dup ON lp.pID = dup.pID
group by lp.pID