Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: how to get records with count > 1?

I have a sql like this:

SELECT *, count(*) as cc 
FROM manytomany 
GROUP BY aid, bid
ORDER BY cc DESC

which return all records with the count #.

however, what can I do if I only want to get the ones with count > 1?

like image 251
Murvinlai Avatar asked Nov 30 '09 22:11

Murvinlai


4 Answers

SELECT *, count(*) as cc 
FROM manytomany 
GROUP BY aid, bid
HAVING 1 < count(*)
ORDER BY cc DESC
like image 124
just somebody Avatar answered Oct 14 '22 14:10

just somebody


You need a HAVING clause, for example:

SELECT *, count(*) as cc 
FROM manytomany 
GROUP BY aid, bid
HAVING COUNT(*) > 1
ORDER BY cc DESC

Here's some background.

like image 45
martin clayton Avatar answered Oct 14 '22 14:10

martin clayton


You use the having clause.

SELECT *, count(*) as cc
FROM manytomany
GROUP BY aid, bid
HAVING count(*) > 1
ORDER BY cc DESC
like image 2
Jason Punyon Avatar answered Oct 14 '22 14:10

Jason Punyon


SELECT *, count(*) as cc 
FROM manytomany 
GROUP BY aid, bid
HAVING COUNT(*) > 1
ORDER BY cc DESC

I don't use MySQL, but it should support HAVING -it has been around for along time.

like image 1
cdonner Avatar answered Oct 14 '22 12:10

cdonner