2015-05-11 26 views
0

中我有一個表是這樣的:MySQL的 - 組一組

doctor_name | medicine 

John      a 
John      b 
John      a 
John      b 
Kevin     c 
Kevin     c 
Kevin     c 
Kevin     a 

我想組雙方doctor_namemedicine。我的願望輸出是SELECT這樣的:

doctor_name | medicine | COUNT(medicine) 
John      a    2 
John      b    2 
Kevin     a    1 
Kevin     c    3 

我不知道如何使用正常GROUP BY MySQL中做到這一點。真的很感謝有人能幫助我。

回答

0

你需要按雙方doctor_namemedicine

select 
doctor_name, 
medicine, 
count(*) as total 
from table_name 
group by doctor_name,medicine 
0
SELECT 
doctor_name, 
medicine, 
count(*) as total_medicine 
FROM table_name 
GROUP BY doctor_name,medicine; 
0

與此查詢

select doctor_name,medicine,count(*) as count 
from my_table group by doctor_name,medicine 
嘗試