I am trying to write a mysql query to select at least two or more than same user subscription.Here's the query:
SELECT a.* FROM `subscription` a, user b WHERE b.id=a.user_id and
count(a.user_id) > 1 group by a.id
Can anyone tell me what i am doing wrong?
You should use having
with Aggregate functions.
Where
is used to filter rows. Having
is used to filter groups based on given condition.
SELECT a.* FROM `subscription` a, user b WHERE b.id=a.user_id
group by a.id
having count(a.user_id) > 1
Also, use ANSI Syntax for Join
as follows:
SELECT a.*
FROM `subscription` a
inner join
user b
on b.id=a.user_id
group by a.id
having count(a.user_id) > 1
;
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