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 に答える