0

シンプルなテーブル

________________________________________________
| id | parent_id | name | some_condition_field |
------------------------------------------------

行セットが欲しい:

________________________________________________________________________________
| id | name | count_childs_with_condition_one | count_childs_with_condition_two |
---------------------------------------------------------------------------------

可能です?変数、UNION など..???

4

1 に答える 1

2

クロスプラットフォーム ソリューションは次のとおりです。

片親の場合:

select count(case when some_condition_field = 'xxx' then 1 end) as CountXXX, 
    count(case when some_condition_field = 'yyy' then 1 end) as CountYYY
from MyTable
where parent_id = 123

保護者の皆様へ:

select parent_id, 
    count(case when some_condition_field = 'xxx' then 1 end) as CountXXX, 
    count(case when some_condition_field = 'yyy' then 1 end) as CountYYY
from MyTable
group by parent_id

MySQL の場合、次の構文も使用できます。

select parent_id, 
    count(if(some_condition_field = 'xxx', 1, null) as CountXXX, 
    count(if(some_condition_field = 'yyy', 1, null)) as CountYYY
from MyTable
group by parent_id
于 2012-07-15T15:05:24.733 に答える