0

次のクエリがあります。

SELECT count(id) from table_1 WHERE field_1 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

これは単一のクエリで実行できますか.. 1 つのテーブルのみが使用されますが、次のような 3 つの出力があります。

    count(id) | count(id) | count(id)<br>
    12        | 44        | 55
4

2 に答える 2

2

はい、次のような CASE 式で集計関数を使用して結果を取得できます。

select
  sum(case when field_1 = 1 then 1 else 0 end) field1Total,
  sum(case when field_2 = 1 then 1 else 0 end) field2Total
from table_1

sum(case...)合計する残りの項目の式をさらに追加します。

于 2013-08-23T16:50:27.743 に答える
0
Select Distinct (Select Count(id) from table_1 where field1=1)as id1, (Select Count(id) from table_1 where field2=1)as id2  from table_1
于 2013-08-23T17:02:27.727 に答える