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?
SELECT *, count(*) as cc
FROM manytomany
GROUP BY aid, bid
HAVING 1 < count(*)
ORDER BY cc DESC
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.
You use the having clause.
SELECT *, count(*) as cc
FROM manytomany
GROUP BY aid, bid
HAVING count(*) > 1
ORDER BY cc DESC
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With