Daniyal Yasin
Daniyal Yasin

Reputation: 172

MySQL Count distinct values from one column

I have a table having three columns:

A   B   C
1   2   2
2   2   2
3   1   1
4   1   2   

I want the count of those values which have C equal to 2 but with distinct values of B

So in this case for C = 2, count = 2 (B=2 and B=1)

I used the following command:

Select count(*) from mytable where C=2 group by (B)

but it yields:

count(*)
3

I have tried using "distinct" but it can't be use to select from one column

Upvotes: 0

Views: 1491

Answers (2)

Adrien Brunelat
Adrien Brunelat

Reputation: 4652

Have you tried

SELECT COUNT(DISTINCT B) FROM mytable WHERE C = 2;

Upvotes: 2

Lamar
Lamar

Reputation: 1849

Use sub query like this:

Select count(*) from (
    select distinct B where c=2
)

Upvotes: 1

Related Questions