4

我想运行一个这样的选择。

SELECT
        COUNT(*) WHERE switch=0 AND detail=1 AS zeroone
    AND
        COUNT(*) WHERE switch=0 AND detail=2 AS zerotwo
    AND
        COUNT(*) WHERE switch=1 AND detail=1 AS oneone
    AND 
        COUNT(*) WHERE swithc=1 AND detail=2 AS onetwo
  FROM tablename

有没有办法做到这一点?

4

5 回答 5

4
SELECT
    sum(switch=0 AND detail=1) AS zeroone,
    sum(switch=0 AND detail=2) AS zerotwo,
    sum(switch=1 AND detail=1) AS oneone,
    sum(switch=1 AND detail=2) AS onetwo
FROM tablename
于 2012-07-31T17:41:49.237 回答
4

您可以CASE在您的语句中使用语句SELECT来获得结果:

SELECT SUM(case when switch=0 AND detail=1 then 1 else 0 end) as zeroone
    , SUM(case when switch=0 AND detail=2 then 1 else 0 end) as zerotwo
    , SUM(case when switch=1 AND detail=1 then 1 else 0 end) as oneone
    , SUM(case when switch=1 AND detail=2 then 1 else 0 end) as onetw
FROM tablename
于 2012-07-31T17:42:37.930 回答
1
SELECT SUM(CASE WHEN switch = 0 and detail = 1 THEN 1 ELSE NULL END) AS zeroone
     , ...
FROM TableName
于 2012-07-31T17:39:54.263 回答
0

目前尚不清楚你想要什么。

我想你想要一个选择,它返回一行,有 4 列:

SELECT
COUNT(*) WHERE switch=0 AND detail=1 AS zeroone,
COUNT(*) WHERE switch=0 AND detail=2 AS zerotwo,
COUNT(*) WHERE switch=1 AND detail=1 AS oneone,
COUNT(*) WHERE swithc=1 AND detail=2 AS onetwo
FROM tablename
于 2012-07-31T17:43:38.570 回答
0

你为什么不用这个?

SELECT COUNT(*) 
  FROM tablename
 GROUP BY switch, detail
于 2012-07-31T17:46:49.537 回答