Java 从列中搜索以获取各种数据的计数

Java 从列中搜索以获取各种数据的计数,java,mysql,jdbc,Java,Mysql,Jdbc,这是我的表格emp: +---------+------------------+ | emp | id | color | +---------+------------------+ | hary | 123 | red | +---------+------------------+ | lary | 125 | green | +---------+------------------+ | gary | 038 |

这是我的表格
emp

+---------+------------------+
| emp     |  id      | color |
+---------+------------------+
| hary    | 123      |  red  |
+---------+------------------+
| lary    | 125      | green |
+---------+------------------+
| gary    | 038      |  red  |
+---------+------------------+
| Kris    | 912      | blue  |
+---------+------------------+
| hary    | 123      |  red  |
+---------+------------------+
| Ronn    | 334      | green |
+---------+------------------+
现在我想知道每种颜色的数量,即红色、绿色和蓝色

在这个上下文中,我试图用
的形式写下查询,其中颜色像“%bl%”、像“%ree%”、像%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中使用它)。 我将非常感谢您对这个问题的任何意见

select color,count(*) clrCount
from emp 
group by color
带where子句

select color,count(*) clrCount
from emp where (color like '%bl%' or color like '%ree%')
group by color
带where子句

select color,count(*) clrCount
from emp where (color like '%bl%' or color like '%ree%')
group by color
你可以使用

select color,count(*) from emp group by color;
你可以使用

select color,count(*) from emp group by color;

但我想在我的查询中使用这种“颜色,如'%bl%'、如'%ree%'、如%ed%”。任何输入您可以在上面的查询中添加where条件以及您在回答中解释的所有条件。选择颜色,将(*)计数为emp where中的CLRCUNT(颜色类似于'%bl%'或颜色类似于'%ree%')按颜色分组。使用as显示headerbrilliant回答anand,感谢社区的支持。但我想在我的查询中使用“类似于“%bl%”、类似于“%ree%”、类似于%ed%”的颜色。任何输入您可以在上面的查询中添加where条件以及您在回答中解释的所有条件。选择颜色,将(*)计数为emp where中的CLRCUNT(颜色类似于'%bl%'或颜色类似于'%ree%')按颜色分组。用于显示headerbrilliant答案,并感谢社区的支持。第一个答案是pivot视图所需的内容。。e、 g.第一个答案是你的透视图需要什么。。例如