3

I wish to find out the count of distinct rows in a MySQL DB.

id | val1 | val2 | val3

1  |  1   |  1   |  1
2  |  1   |  1   |  1
3  |  2   |  2   |  2
4  |  2   |  2   |  2

On the table above the query would return

val1 | val2 | val3 | count
1    |   1  |  1   |   2 
2    |   2  |  2   |   2

Does anyone know a reasonably efficient way of achieving this in MySQL.

1 Answer 1

5

You need to use GROUP BY clause for this:

SELECT val1, val2, val3, count(*) AS count
FROM mytable GROUP BY val1, val2, val3

See this fiddle

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

1 Comment

@marabutt and Spechal it is really nice tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.