[英]Select where count of one field is greater than one
I want to do something like this:我想做这样的事情:
SELECT *
FROM db.table
WHERE COUNT(someField) > 1
How can I achieve this in MySql?如何在 MySql 中实现这一点?
Use the HAVING
, not WHERE
clause, for aggregate result comparison. 使用HAVING
而不是WHERE
子句进行聚合结果比较。
Taking the query at face value: 以面值进行查询:
SELECT *
FROM db.table
HAVING COUNT(someField) > 1
Ideally, there should be a GROUP BY
defined for proper valuation in the HAVING
clause, but MySQL does allow hidden columns from the GROUP BY ... 理想情况下,应该在HAVING
子句中定义GROUP BY
以进行适当的估值,但MySQL确实允许来自GROUP BY的隐藏列 ...
Is this in preparation for a unique constraint on someField
? 这是为了准备在someField
上的唯一约束吗? Looks like it should be... 看起来应该是......
SELECT username, numb from(
Select username, count(username) as numb from customers GROUP BY username ) as my_table
WHERE numb > 3
You can also do this with a self-join: 你也可以通过自我加入来做到这一点:
SELECT t1.* FROM db.table t1
JOIN db.table t2 ON t1.someField = t2.someField AND t1.pk != t2.pk
Here you go: 干得好:
SELECT Field1, COUNT(Field1)
FROM Table1
GROUP BY Field1
HAVING COUNT(Field1) > 1
ORDER BY Field1 desc
One way 单程
SELECT t1.*
FROM db.table t1
WHERE exists
(SELECT *
FROM db.table t2
where t1.pk != t2.pk
and t1.someField = t2.someField)
我在Sql中的两个表之间给出了一个示例:
Select cn.name,ct.name,count(ct.id) totalcity from city ct left join country cn on ct.countryid = cn.id Group By cn.name,ct.name Having totalcity > 2
As OMG Ponies stated, the having clause is what you are after. 正如OMG小马所说,拥有条款就是你所追求的。 However, if you were hoping that you would get discrete rows instead of a summary (the "having" creates a summary) - it cannot be done in a single statement. 但是,如果您希望获得离散行而不是摘要(“有”创建摘要) - 则无法在单个语句中完成。 You must use two statements in that case. 在这种情况下,您必须使用两个语句。
For me, Not having a group by just returned empty result. 对我来说,没有一个组只返回空结果。 So i guess having a group by for the having statement is pretty important 所以我觉得拥有一个group by for having语句非常重要
Without HAVING<\/strong>没有<\/strong>
SELECT COL,TOTAL
FROM (SELECT SPORT, COUNT(COL) AS TOTAL FROM db.table GROUP BY SPORT)
WHERE TOTAL > 100 ORDER BY TOTAL
Maybe:也许:
SELECT *
FROM [Table]
where field IN (
select field
from [Table]
group by field
having COUNT(field)>1 )
It should also be mentioned that the "pk" should be a key field. 还应该提到“pk”应该是一个关键领域。 The self-join 自我加入
SELECT t1.* FROM db.table t1
JOIN db.table t2 ON t1.someField = t2.someField AND t1.pk != t2.pk
by Bill Karwin give you all the records that are duplicates which is what I wanted. 作者Bill Karwin为您提供所有重复的记录,这就是我想要的。 Because some have more than two, you can get the same record more than once. 因为有些人有两个以上,你可以多次获得相同的记录。 I wrote all to another table with the same fields to get rid of the same records by key fields suppression. 我写了所有到具有相同字段的另一个表,以通过关键字段抑制来消除相同的记录。 I tried 我试过了
SELECT * FROM db.table HAVING COUNT(someField) > 1
above first. 首先是上面。 The data returned from it give only one of the duplicates, less than 1/2 of what this gives you but the count is good if that is all you want. 从它返回的数据只给出其中一个副本,不到它给你的1/2,但如果这就是你想要的那么计数是好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.