我有一个mysql表,其中包含两列'test-id'和'q-id'。 'test-id'范围是1到160,test-id
的每个值都有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
我想计算哪个test-id有多少q-id。如何在单个查询中执行此操作?
答案 0 :(得分:2)
要理解的关键语句是GROUP BY语句。
SELECT "test-id", COUNT(DISTINCT "q-id") as qid_count FROM "test" GROUP BY "test-id"
答案 1 :(得分:2)
SELECT testid, COUNT(DISTINCT q-id) from test Group by testid