SQL case子查询+子查询> 0

时间:2014-09-17 11:46:52

标签: mysql sql case

我试图用另一个数字来表示一个数字。但是因为结果有可能是0我想说一个案例,如果它是0那么它应该是1,因为我创建了这个:

(CASE(SELECT COUNT(*) 
     FROM module_score 
     WHERE user_id = 40 AND medal_id > 1) 
+ 
    (SELECT COUNT(*) 
     FROM user_has_module_score uhms 
     WHERE user_id = 40 and medal_id > 1)> 0 
THEN 1 
ELSE 0 END) as passed_percentage

但是我收到以下语法错误:

medal_id > 1)> 0 THEN 1 ELSE 0 END) as passed_percentage  
FROM system_learningbank.user U 
WHERE U.id = 40 GROUP BY U.id   

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'THEN 1 ELSE 0 END) as passed_percentage FROM system_learningbank.user U WH' at line 7

那么如果验证它不是0,那么正确的语法是什么?在上面的示例中,我将变量设置为1,但这应该是子查询的实际计数

我的完整sql语句

    SELECT 
    (SELECT 
            COUNT(*)
        FROM
            module_score MS
        WHERE
            user_id = 40) + (SELECT 
            COUNT(UHMS.score)
        FROM
            user_has_module_score UHMS
        WHERE
            UHMS.user_id = 40) / ((SELECT 
            COUNT(*)
        FROM
            module_score
        WHERE
            user_id = 40 AND medal_id > 1) + (SELECT 
            COUNT(*)
        FROM
            user_has_module_score uhms
        WHERE
            user_id = 40 and medal_id > 1) > 0) as passed_percentageas
FROM
    system_learningbank.user U
WHERE
    U.id = 40
GROUP BY U.id;

1 个答案:

答案 0 :(得分:3)

您错过了when

(CASE WHEN (SELECT COUNT(*) FROM module_score WHERE user_id = 40 AND medal_id > 1) +
           (SELECT COUNT(*) FROM user_has_module_score uhms WHERE user_id = 40 and medal_id > 1) > 0
      THEN 1 ELSE 0
 END) as passed_percentage

MySQL将布尔值视为整数,因此您实际上可以在不使用case的情况下编写布尔值:

( (SELECT COUNT(*) FROM module_score WHERE user_id = 40 AND medal_id > 1) +
  (SELECT COUNT(*) FROM user_has_module_score uhms WHERE user_id = 40 and medal_id > 1) > 0
) as passed_percentage

编辑:

   SELECT (SELECT COUNT(*)
           FROM module_score MS
           WHERE user_id = 40
          ) +
          (SELECT COUNT(UHMS.score)
           FROM user_has_module_score UHMS
           WHERE UHMS.user_id = 40
          ) / ((SELECT COUNT(*)
                FROM module_score
                WHERE user_id = 40 AND medal_id > 1
               ) +
               (SELECT COUNT(*)
                FROM user_has_module_score uhms
                WHERE user_id = 40 and medal_id > 1
               ) > 0
              ) as passed_percentageas
FROM system_learningbank.user U
WHERE U.id = 40
GROUP BY U.id;

我怀疑你想要更像这样的东西:

   SELECT ((ms.cnt + hms.cnt) /
           (case when ms.cnt_m1 + hms.cnt_m2 > 0 then ms.cnt_m1 + hms.cnt_m2 end)
          ) as passed_percentageas
FROM system_learningbank.user U left join
     (select userid, count(*) as cnt, sum(medal_id > 1) as cnt_m1
      from module_score
      group by user_id
     ) ms
     on ms.user_id = u.user_id left join
     (select userid, count(*) as cnt, sum(medal_id > 1) as cnt_m1
      from user_has_module_score
      group by user_id
     ) hms
     on hms.user_id = u.user_id
WHERE U.id = 40;

然而,这仍然看起来很可疑。 。 。表名表明只有一个真正编码你想要的分数,分母比分子更具限制性,但这种结构基本上是你想要的。     GROUP BY U.id;