2

我的表包含pk_id, reviewer_id, rating。有 4 种类型的评级。

1-very good.
2-good.
3-bad.
4-very bad.

我想计算每个评论者给出的评分。意思是:如果拥有 id 200 的 Akee 对不同的代码给出了 2 非常好、4 好、3 差和零非常差的评级。

我要结果

  count--- rate

   2---------1

   4---------2

   3---------3

   0---------4

我的查询是

SELECT COUNT(RATE),RATE
  FROM CODE_REVIEW WHERE CODE_REVIEWER_ID= 200
 GROUP BY RATE;

它正在显示结果

  count--- rate

   2---------1

   4---------2

   3---------3

我想显示第四行,即 4 评级为零。如何做呢??

4

6 回答 6

1

如果 Rate 不是另一个表中的主键,那么您需要定义自己的速率列表,以便 MySQL 知道排列rate是什么:

SELECT  Rates.Rate,
        COUNT(Code_Review.Rate) AS CountOfRate
FROM    (   SELECT 1 AS Rate UNION ALL
            SELECT 2 AS Rate UNION ALL
            SELECT 3 AS Rate UNION ALL
            SELECT 4
        ) AS Rates
        LEFT JOIN Code_Review
            ON Code_Review.Rate = Rates.Rate
            AND CODE_REVIEWER_ID = 200
GROUP BY Rates.Rate
于 2012-07-04T07:45:03.393 回答
0

假设您没有定义费率的单独表。

SElECT * from (
  SELECT distinct(m.rate), countrate from code_review m 
  LEFT JOIN
  (SELECT COUNT(rate) as countrate,rate FROM code_review 
     WHERE code_reviewer_id=200 GROUP BY rate) t 
  ON m.rate=t.rate) a
于 2012-07-04T07:41:56.323 回答
0

如果可以,您应该尝试以列格式获取它,因为它很简单:

SELECT
    SUM(rate = 1) AS 1,
    SUM(rate = 2) AS 2,
    SUM(rate = 3) AS 3,
    SUM(rate = 4) AS 4
FROM
    code_review
WHERE
    code_reviewer_id = 200

但是如果你真的需要一个行格式,你可以这样做:

SELECT
    a.rate,
    COUNT(b.rate) AS cnt
FROM
    (
        SELECT 1 AS rate UNION ALL
        SELECT 2 AS rate UNION ALL
        SELECT 3 AS rate UNION ALL
        SELECT 4 AS rate
    ) a
LEFT JOIN
    code_review b ON a.rate = b.rate AND code_reviewer_id = 200
GROUP BY
    a.rate
于 2012-07-04T07:42:23.550 回答
0

你可以这样做

SELECT
            rates.RATE
            , SUM(COUNT) COUNT
   FROM
           (
           SELECT 1 RATE, 0 COUNT UNION ALL
           SELECT 2 RATE, 0 COUNT UNION ALL
           SELECT 3 RATE, 0 COUNT UNION ALL
           SELECT 4 RATE, 0 COUNT
           ) Rates
       LEFT JOIN
           (
               SELECT
                         RATE
                       , COUNT(RATE) COUNT 
                   FROM
                       CODE_REVIEW 
                   WHERE
                       CODE_REVIEWER_ID= 200
                   GROUP BY RATE
           ) Ratings200
           ON Ratings200.RATE = Rates.RATE
于 2012-07-04T07:45:05.463 回答
0

试试这个查询:

SELECT coalesce(c.cnt, 0), r.rate
  FROM (SELECT 1 AS rate UNION ALL SELECT 2
        UNION ALL SELECT 3 UNION ALL SELECT 4) AS r
  LEFT JOIN (SELECT COUNT(RATE),RATE
          FROM CODE_REVIEW WHERE CODE_REVIEWER_ID= 200
         GROUP BY RATE) AS c
    ON r.rate = c.rate;
  1. 第一个子查询创建一个可能的费率列表。如果您有一个定义所有费率的表格,则可以避免它;
  2. 第二个子查询是你的;
  3. LEFT JOIN保证显示所有费率;
  4. coalesce()需要转换NULL为0。
于 2012-07-04T07:46:10.813 回答
0
SELECT 
    Rate,
    totCount
FROM 
(
    Select
        Rate,
        count(Rate) as totCount
    from
        Code_Review 
    where 
        CODE_REVIEWER_ID  = 200  
    group by 
        Rate
    union
    select  4, 0  
    union
    select  3, 0  
    union
    select  2, 0  
    union
    select  1, 0 
) AS T 
group by  
    T.Rate
于 2012-07-06T05:28:47.483 回答