4

E.g:

with the select query I got below result:

columnA columnB
type1   typea
type2   typea
type3   typeb
type4   typec
type5   typed
type6   typed
type7   typed

with the DISTINCT I only got the distinct result,but I also want to get the total number of each distinct.

and now I want to get the total number of

typea,typeb,typec and typed.

Just like:

columnB total
typea   2
typeb   1
typec   1
typed   3

Thank you very much!!

1 Answer 1

9

You can use GROUP BY to get results by type:

SELECT columnB, COUNT(*) AS 'total'
  FROM myTable
  GROUP BY columnB;

This should give you exactly the result you are looking for.

MySQL Documentation: 11.16.1 GROUP BY (Aggregate) Functions

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.