根据第一列的重复值匹配第二列的重复值

Match duplicate value from the second column based on duplicate value from first colum

我有table这样的

CREATE TABLE table1 (
  `ID` VARCHAR(100),
  `Val` VARCHAR(100),
  `Val2` VARCHAR(100)
);

INSERT INTO table1
  (`ID`, `Val`, `Val2`)
VALUES
  ('1','4788','90'),
  ('2','4788','90'),
  ('10','4788','10'),
  ('20','111','10'),
  ('30','111','10'),
  ('57','89','89111'),
  ('59','89','89111'),
  ('60','89','10'),
  ('2','900','7000'),
  ('4','900','7001');

对于这个 table 我有两个条件,即:

  1. Val 列必须是重复值 AND,
  2. Val2 列必须重复

所以我的目标是 return 如果两个条件都满足的数据。如果列 val 有重复值并且列 val2 有重复值并且每列在他自己的行上。

我的查询是这样的

select t1.* from table1 t1
where exists (select 1 from table1 where id <> t1.id and val = t1.val)
and exists (
  select 1 from table1 
  where val = t1.val and val2 in (select val2 from table1 group by val2 having count(*) > 1)
)

结果是这样的

ID  Val Val2
1   4788    90
2   4788    90
10  4788    10
20  111 10
30  111 10
57  89  89111
59  89  89111
60  89  10

如您所见,列彼此不匹配

我希望结果数据是这样的

ID  Val Val2
1   4788    90
2   4788    90
20  111 10
30  111 10
57  89  89111
59  89  89111

这是我的 fiddle

您可以起诉 group by 并要求检查重复项

    select  val, val2 
    from  table1 

    group by val, val2 
    having count(*) > 1   

然后

select t1.* 
from table1 t1
inner join (
    select  val, val2 
    from  table1 

    group by val, val2 
    having count(*) > 1  
) t on t.val =t1.val and t.val2 = t1.val2

您需要 havingjoin。这是 demo.

select 
   t.* 
from table1 t
join (
    select  
       val, val2 
    from table1 
    group by 
       val, val2 
    having count(*) > 1  
) t1 
on t.val = t1.val 
and t.val2 = t1.val2

输出:

| ID  | Val  | Val2  |
| --- | ---- | ----- |
| 1   | 4788 | 90    |
| 2   | 4788 | 90    |
| 20  | 111  | 10    |
| 30  | 111  | 10    |
| 57  | 89   | 89111 |
| 59  | 89   | 89111 |

exists 的方向是正确的。你只是把逻辑复杂化了:

select t1.*
from table1 t1
where exists (select 1
              from table1 tt1
              where tt1.id <> t1.id and
                    tt1.val = t1.val and
                    tt1.val2 = t1.val2
             ) ;

您想要两个值的副本在同一行。因此,这是一个 exists 子句。

Here 是一个 db<>fiddle.