0
SELECT  COUNT(ID) AS RANK1 FROM `db_ranking` WHERE `sid`=2 AND `rank`=1
UNION
SELECT  COUNT(ID) AS RANK2  FROM `db_ranking` WHERE `sid`=2 AND `rank`=2
UNION
SELECT  COUNT(ID) AS RANK3  FROM `db_ranking` WHERE `sid`=2 AND `rank`=3
UNION
SELECT   COUNT(ID)  AS RANK4  FROM `db_ranking` WHERE `sid`=2 AND `rank`=4
UNION
SELECT COUNT(ID) AS RANK5 FROM `db_ranking` WHERE `sid`=2 AND `rank`=5

すべての SELECT 結果を列に表示したい..

だから私は5つの列が欲しい.各列にはIDの数がある..

だからそうあるべき

RANK1 RANK2 RANK3 RANK4 RANK5
  0     3     5     6     7

私は今それを得ていません..そして私は理由を知りません

4

2 に答える 2

1

以下は正常に動作します..

  select
    (SELECT  COUNT(ID) AS RANK1 FROM `db_ranking` WHERE `sid`=2 AND `rank`=1) as Rank1,
    (SELECT  COUNT(ID) AS RANK2  FROM `db_ranking` WHERE `sid`=2 AND `rank`=2) as rank2,
    (SELECT  COUNT(ID) AS RANK3  FROM `db_ranking` WHERE `sid`=2 AND `rank`=3) as rank3,
    (SELECT   COUNT(ID)  AS RANK4  FROM `db_ranking` WHERE `sid`=2 AND `rank`=4) as rank4,
    (SELECT COUNT(ID) AS RANK5 FROM `db_ranking` WHERE `sid`=2 AND `rank`=5) as rank5
    from dual
于 2012-08-02T10:08:31.267 に答える
0

最も効率的な方法は

SELECT
    SUM(CASE WHEN `rank`=1 THEN 1 ELSE 0 END) as Rank1,
    SUM(CASE WHEN `rank`=2 THEN 1 ELSE 0 END) as Rank2,
    SUM(CASE WHEN `rank`=3 THEN 1 ELSE 0 END) as Rank3,
    SUM(CASE WHEN `rank`=4 THEN 1 ELSE 0 END) as Rank4,
    SUM(CASE WHEN `rank`=5 THEN 1 ELSE 0 END) as Rank5
FROM
    `db_ranking`
WHERE
    `sid`=2
于 2012-08-02T10:46:59.847 に答える