简体   繁体   English

MYSQL 不同列行对查询

[英]MYSQL distinct column row pair query

Imagine a table of contacts, where the same contact has multiple entries, but with differing data.想象一个联系人表,其中同一个联系人有多个条目,但数据不同。 How would one go about selecting this data for review? go 将如何选择此数据进行审查? Unfortunately, a merge of sorts would be disagreeable as there may exist visually identifiable erroneous data that is not currently envisaged to be automatically processed.不幸的是,由于可能存在视觉上可识别的错误数据,这些数据目前没有被设想为自动处理,因此各种类型的合并将是令人不快的。

FName         LName           Email           Phone
Heywood       Yapinchme                       555-555-555
Heywood       Yapinchme       hy@moes.com               
Seymour       Butz            sb@moes.com   
Seymour       Butz                            555-555-556
Seymour       Butz            
Hughe         Jass            hj@moes.com     555-555-557
Amanda        Hugginkiss      ah@moes.com

I would like to see just the duplicates of the first two columns where more than one entry exists.我想只查看存在多个条目的前两列的重复项。 ie IE

FName         LName           Email           Phone
Heywood       Yapinchme                       555-555-555
Heywood       Yapinchme       hy@moes.com               
Seymour       Butz            sb@moes.com   
Seymour       Butz                            555-555-556
Seymour       Butz            

The next step of review is in the blue ether.审查的下一步是在蓝醚中。 Currently a little over a million rows, Bart has been busy.目前有超过一百万行,Bart 一直很忙。 But beefy servers and this isn't regular operation but a one off to deal with data migration, so can be slightly gash.但是强大的服务器,这不是常规操作,而是处理数据迁移的一次性操作,因此可能会有些小问题。

I have tried a bit with SELECT DISTINCT and GROUP BY but it seems to just return on of each contact.我已经尝试过使用SELECT DISTINCTGROUP BY但它似乎只是返回每个联系人。

You can use aggregation to identify the duplicates:您可以使用聚合来识别重复项:

SELECT FName, LName
FROM tablename
GROUP BY FName, LName
HAVING COUNT(*) > 1

and if you want all the rows of the duplicates:如果你想要重复的所有行:

SELECT *
FROM tablename
WHERE (FName, LName) IN (
  SELECT FName, LName
  FROM tablename
  GROUP BY FName, LName
  HAVING COUNT(*) > 1
)

If your MySql/MariaDB version supports window functions:如果您的 MySql/MariaDB 版本支持 window 函数:

SELECT t.*
FROM (
  SELECT *, COUNT(*) OVER (PARTITION BY FName, LName) counter
  FROM tablename
) t
WHERE t.counter > 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM