这是我的桌子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 中使用它)。我将不胜感激有关此问题的任何意见。