在3列SQL中查找重复值

时间:2018-08-17 16:53:53

标签: mysql sql

我有一个如下表:

Site | Name | ID
A | Mike | 1
A | Mary | 2
A | Mary | 3
B | Mary | 1
B | Rich | 2

我想在站点中找到所有重复的名称。所以我想返回:

Site | Name | ID
A | Mary | 2
A | Mary | 3

我已经尝试过了:

SELECT DISTINCT Site, Name, ID
from table
group by ID having count(*) > 1

由于将站点A和站点B一起计数,结果错误地返回。我只想在每个站点中找到重复项,而不能在各个站点中找到重复项。

2 个答案:

答案 0 :(得分:0)

您可以使用var utterance = AVSpeechUtterance() var synthesizer = AVSpeechSynthesizer() utterance = AVSpeechUtterance(string: "\(textCount[modeIndex][0])") utterance.voice = AVSpeechSynthesisVoice(language: "en-US") utterance.rate = 0.4 synthesizer = AVSpeechSynthesizer() synthesizer.speak(utterance) --

exists

答案 1 :(得分:0)

您可以尝试对子查询NOT exists使用having

网站中的重复名称

CREATE TABLE T(
  Site varchar(5),
  Name varchar(5),
  ID int
);



insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);

查询1

SELECT * FROM T t1 WHERE 
NOT exists
( 
  SELECT 1 
  FROM T tt
   where t1.name = tt.name 
  group by Name
  HAVING MIN(tt.ID) = t1.ID
)

Results

| Site | Name | ID |
|------|------|----|
|    A | Mary |  2 |
|    A | Mary |  3 |

在每个网站内查找重复项

CREATE TABLE T(
  Site varchar(5),
  Name varchar(5),
  ID int
);



insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);

查询1

SELECT t1.* 
FROM T t1 
WHERE not exists
( 
  SELECT 1 
  FROM T tt
  where t1.name = tt.name and t1.Site = tt.Site
  group by Name,Site
  HAVING MIN(tt.ID) = t1.ID
)

Results

| Site | Name | ID |
|------|------|----|
|    A | Mary |  3 |