I want to do something like:
SELECT *
FROM db.table
WHERE COUNT(someField) > 1
how do I do this in MySql.
I want to do something like:
SELECT *
FROM db.table
WHERE COUNT(someField) > 1
how do I do this in MySql.
Use the HAVING
, not WHERE
clause, for aggregate result comparison.
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...
Is this in preparation for a unique constraint on someField
? Looks like it should be...
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)
As OMG Ponies stated, the having clause is what you are after. 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.
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