[英]How to select non “unique” rows
I have the following table, from which i have to fetch non unique rows 我有下表,我必须从中获取非唯一行
+------+------+------+
| id | idA |infos |
+----- +------+------+
| 0 | 201 | 1899 |
| 1 | 205 | 1955 |
| 2 | 207 | 1955 |
| 3 | 201 | 1959 |
+------+------+------+
I'd like fetch all the rows for the column infos
, that have a same idA
value in at least two rows. 我想获取列infos
所有行,它们在至少两行中具有相同的idA
值。
Output of the query for the above table must be 必须输出上表的查询
infos
1899
1959
I've tried the following requests with no success : 我尝试了以下请求但没有成功:
SELECT idA FROM XXX WHERE NOT EXISTS(SELECT * FROM XXX GROUP BY idA)
SELECT * FROM XXX a WHERE NOT EXISTS(SELECT * FROM XXX b WHERE a.RVT_ID=b.RVT_ID GROUP BY idA)
Try this: 尝试这个:
SELECT T1.idA, T1.infos
FROM XXX T1
JOIN
(
SELECT idA
FROM XXX
GROUP BY idA
HAVING COUNT(*) >= 2
) T2
ON T1.idA = T2.idA
The result for the data you posted: 您发布的数据的结果:
idaA infos 201 1899 201 1959
这样的事情应该有效:
SELECT idA, COUNT(*) FROM XXX GROUP BY idA HAVING COUNT(*) > 1
SELECT id, idA, COUNT(infos) AS cnt
FROM mytable
GROUP BY infos
HAVING cnt > 1
这可能是你正在寻找的:
SELECT *, COUNT(DISTINCT infos) FROM table GROUP BY idA HAVING COUNT(DISTINCT infos) > 1;
This should give all the rows where "infos" appear exactly once (Tested on MySQL) 这应该给出“infos”只出现一次的所有行(在MySQL上测试)
SELECT id, idA, count(infos) AS cnt
FROM mytable
GROUP BY infos
HAVING cnt=1
Data 数据
id ida infos
1 201 1955
2 202 1978
3 203 1978
4 204 1956
5 0 1980
Result 结果
id idA cnt
1 201 1
4 204 1
5 0 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.