1

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

SELECT COUNT(DISTINCT ..) instead SELECT DISTINCT COUNT(id) - well. Thank you, will be accepted asap.
Happy to help :) SELECT DISTINCT COUNT can't work because you'd select a COUNT first (yielding one result), and then DISTINCTing it (yielding exactly the same count).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.