Lucas
Lucas

Reputation: 3502

Get count of ids

I have a table images - foo_id isn't a key, can have more than one row with the same number.
How do I count the rows with different values for foo_id?

images:

id (int) | foo_id (int) | ...

Example:

0 | 1 | ...
0 | 2 | ...
0 | 3 | ...
0 | 1 | ...

Wanted result:
3

Upvotes: 1

Views: 109

Answers (1)

Uli Köhler
Uli Köhler

Reputation: 13751

Use SELECT COUNT(DISTINCT ...)

SELECT COUNT(DISTINCT foo_id) FROM images

Also see http://www.w3schools.com/sql/sql_func_count.asp, several variants of the COUNT statement (including the COUNT(DISTINCT ...) syntax you need in your case are described there)

Upvotes: 1

Related Questions