0

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

4

2 回答 2

3
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
于 2012-11-19T12:25:11.007 回答
0

您可以使用

select color,count(*) from emp group by color;
于 2012-11-19T12:38:43.727 回答