0

I have a table with this structure

+----------+----------+
| user_id  | tema_id  |
+----------+----------+
|        1 |    1     |
|        2 |    1     |
|        3 |    2     |
|        4 |    3     |
|        5 |    2     |
|        6 |    3     |
|        7 |    1     |
+----------+----------+

What I want to get in only one query is the total of different tema_id by tema_id. I have this query but it returns the different tema_id but the count column to one instead of the total of that tema_id.

SELECT tema_id, COUNT(DISTINCT(tema_id)) as total
FROM push_subscriptions
GROUP BY tema_id

Return this:

+----------+----------+
| tema_id  | total    |
+----------+----------+
|        1 |    1     | -> must be 3
|        2 |    1     | -> must be 2
|        3 |    1     | -> must be 2
+----------+----------+

Thank you

1
  • 1
    Replace COUNT(DISTINCT(tema_id)) to COUNT(tema_id). Commented Mar 5, 2022 at 18:05

1 Answer 1

2

A simple count(*) should do the trick:

select tema_id, count(*) as total
from push_subscriptions
group by tema_id;

Fiddle

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

Comments

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.