MySQL Forums
Forum List  »  Newbie

Counting Column With Having Statement
Posted by: Stefan Chrobak
Date: March 13, 2017 07:13AM

Hi, i have a table "users" with this Columns:

id -> Primary Key
name -> VARCHAR
shop_id -> INTEGER
code -> varchar unique

also have a table shops with this columns
id -> primary key
name -> VARCHAR

The users.shop_id is the ID from table shops

Now i want count in users the shop_id where user
only have two times the same shop_id

Example:

Users:

id name shop_id code
1 max 1 1234
2 max 1 2345
3 tom 2 3456
4 pete 3 4567
5 jane 4 5678
6 jane 4 6789

the result must be '2' because there are
two users who are using two times the the same shop_id

I try to build a statement like this:


SELECT count(id) as count
FROM users
GROUP BY shop_id
HAVING count(shop_id) = 2


But this is wrong, because it is counting each user
and gives back:

count
2
2


but i expect:
count
2

because there are 2 users who using two times the sam e shop_id
How can i write this statement?

Options: ReplyQuote


Subject
Written By
Posted
Counting Column With Having Statement
March 13, 2017 07:13AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.