的計數這裏是我的表emp
:搜索,獲得的各種數據
+---------+------------------+
| emp | id | color |
+---------+------------------+
| hary | 123 | red |
+---------+------------------+
| lary | 125 | green |
+---------+------------------+
| gary | 038 | red |
+---------+------------------+
| Kris | 912 | blue |
+---------+------------------+
| hary | 123 | red |
+---------+------------------+
| Ronn | 334 | green |
+---------+------------------+
現在我想找出每種顏色即紅,綠,藍的計數;
在這方面,我試着寫下來的where color like '%bl%',like '%ree%',like %ed%.
方面的查詢,並希望這一結果
+--------------------------+
| red | blue | green |
+--------------------------+
| 3 | 1 | 2 |
+--------------------------+
我已經試過這件事情:
select count(case when color='green' then 1 end) as Green,count(case when
color='blue' then 1 end) as Blue,count(case when color='Red' then 1 end) as Red from emp;
我不想硬編碼他們的名字(因爲我將在我的代碼jdbc中使用它)。 我會很感激任何關於這個問題的意見。
第一個答案是,你需要爲您透視圖..例如[SQLFiddle](http://sqlfiddle.com/#!2/d4131/7) – bonCodigo