0

I have a mysql table with two columns 'test-id' and 'q-id'. 'test-id' range is from 1 to 160 and each value of those test-id has upto 100 'q-id'.

ex: test-id   q-id
    1         126    #first row
    1         134    #second row
    ...
    1         66    #hundred row

SELECT COUNT(DISTINCT `q-id`)
             from `test`
             WHERE `test-id` = 1;  #answer is 100

I want to count which test-id has how many q-id. how to do it in a single query?

2 Answers 2

2

The key statement to understand is the GROUP BY statement.

SELECT "test-id", COUNT(DISTINCT "q-id") as qid_count FROM "test" GROUP BY "test-id"
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT testid, COUNT(DISTINCT q-id) from test Group by testid

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.