显示所有具有相同值大于 1 的分组值
Show all the groupped value that have same value more than 1
我有table这样的
table1
| ID | Val | Val2 |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 4 | AD | 0 |
| 5 | CV | 1 |
| 6 | AF | 1 |
| 7 | CV | 1 |
我想知道 Val
列中的重复值是否不止一个。我使用了 group by
子句。另外我想知道有多少重复出现在 Val
select Val,count(Val) from table
group by Val where
having count(val) > 1
结果:
| Val | count(val) |
| AA | 2 |
| CV | 2 |
现在我想知道哪一列有重复值,所以我使用了 Group_concat 这样的查询
select Val,count(Val), group_concat(ID) from table1
group by Val where
having count(val) > 1
结果
| Val | count(val) | group_concat(ID) |
| AA | 2 | 2,3 |
| CV | 2 | 5,7 |
现在我不知道如何显示所有重复值,我只通过 group_concat() 显示哪个 ID 具有重复值,但没有 group_concat
列我无法显示所有数据.我尝试使用 Field_in_set
但它似乎不起作用。
select Val,count(Val), group_concat(ID) from table1
where FIELD_IN_SET(ID,group_concat(ID))
group by Val where
having count(val) > 1
我希望在 group by
和 count
之后显示所有重复值,如下所示 table
| ID | Val | Val2 |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 5 | CV | 1 |
| 7 | CV | 1 |
CREATE TABLE table1 (
`ID` INTEGER,
`Val` VARCHAR(2),
`Val2` INTEGER
);
INSERT INTO table1
(`ID`, `Val`, `Val2`)
VALUES
('2', 'AA', '0'),
('3', 'AA', '1'),
('4', 'AD', '0'),
('5', 'CV', '1'),
('6', 'AF', '1'),
('7', 'CV', '1');
SELECT t1.*
FROM table1 t1
INNER JOIN (SELECT COUNT(`Val`) countval, `Val` FROM table1 GROUP BY `Val`) t2
ON t1.`Val` = t2.`Val`
WHERE countval > 1
ID | Val | Val2
-: | :-- | ---:
2 | AA | 0
3 | AA | 1
5 | CV | 1
7 | CV | 1
SELECT `ID`, `Val`, `Val2`
FROM ( SELECT
`ID`, `Val`, `Val2`,
COUNT(`Val`) OVER(PARTITION BY `Val`) c1
FROM table1) t1
WHERE c1 > 1
ID | Val | Val2
-: | :-- | ---:
2 | AA | 0
3 | AA | 1
5 | CV | 1
7 | CV | 1
db<>fiddle here
如果您想 return 具有重复 Val
的行,则使用 EXISTS
:
select t.* from tablename t
where exists (select 1 from tablename where Val = t.Val and ID <> t.ID)
参见demo。
结果:
| ID | Val | Val2 |
| --- | --- | ---- |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 5 | CV | 1 |
| 7 | CV | 1 |
我有table这样的
table1
| ID | Val | Val2 |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 4 | AD | 0 |
| 5 | CV | 1 |
| 6 | AF | 1 |
| 7 | CV | 1 |
我想知道 Val
列中的重复值是否不止一个。我使用了 group by
子句。另外我想知道有多少重复出现在 Val
select Val,count(Val) from table
group by Val where
having count(val) > 1
结果:
| Val | count(val) |
| AA | 2 |
| CV | 2 |
现在我想知道哪一列有重复值,所以我使用了 Group_concat 这样的查询
select Val,count(Val), group_concat(ID) from table1
group by Val where
having count(val) > 1
结果
| Val | count(val) | group_concat(ID) |
| AA | 2 | 2,3 |
| CV | 2 | 5,7 |
现在我不知道如何显示所有重复值,我只通过 group_concat() 显示哪个 ID 具有重复值,但没有 group_concat
列我无法显示所有数据.我尝试使用 Field_in_set
但它似乎不起作用。
select Val,count(Val), group_concat(ID) from table1
where FIELD_IN_SET(ID,group_concat(ID))
group by Val where
having count(val) > 1
我希望在 group by
和 count
之后显示所有重复值,如下所示 table
| ID | Val | Val2 |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 5 | CV | 1 |
| 7 | CV | 1 |
CREATE TABLE table1 ( `ID` INTEGER, `Val` VARCHAR(2), `Val2` INTEGER ); INSERT INTO table1 (`ID`, `Val`, `Val2`) VALUES ('2', 'AA', '0'), ('3', 'AA', '1'), ('4', 'AD', '0'), ('5', 'CV', '1'), ('6', 'AF', '1'), ('7', 'CV', '1');
SELECT t1.* FROM table1 t1 INNER JOIN (SELECT COUNT(`Val`) countval, `Val` FROM table1 GROUP BY `Val`) t2 ON t1.`Val` = t2.`Val` WHERE countval > 1
ID | Val | Val2 -: | :-- | ---: 2 | AA | 0 3 | AA | 1 5 | CV | 1 7 | CV | 1
SELECT `ID`, `Val`, `Val2` FROM ( SELECT `ID`, `Val`, `Val2`, COUNT(`Val`) OVER(PARTITION BY `Val`) c1 FROM table1) t1 WHERE c1 > 1
ID | Val | Val2 -: | :-- | ---: 2 | AA | 0 3 | AA | 1 5 | CV | 1 7 | CV | 1
db<>fiddle here
如果您想 return 具有重复 Val
的行,则使用 EXISTS
:
select t.* from tablename t
where exists (select 1 from tablename where Val = t.Val and ID <> t.ID)
参见demo。
结果:
| ID | Val | Val2 |
| --- | --- | ---- |
| 2 | AA | 0 |
| 3 | AA | 1 |
| 5 | CV | 1 |
| 7 | CV | 1 |