SQL WHERE子句错误

时间:2018-02-06 00:32:29

标签: sql

我想将下面的where子句放在totalscore> 50 但是当我在订单之后放where totalscore > 50时会出现一些错误。

SELECT 
    h.hacker_id, h.name, SUM(ss.maxscore) totalscore
FROM
    (SELECT 
         hacker_id, challenge_id, MAX(score) maxscore
     FROM 
         submission_table s
     GROUP BY 
         challenge_id, hacker_id) AS ss
JOIN 
    hacker_table h ON ss.hacker_id = h.hacker_id
GROUP BY 
    h.hacker_id
ORDER BY 
    totalscore DESC, hacker_id ASC
WHERE 
    totalscore > 50

2 个答案:

答案 0 :(得分:2)

关于WHERE的其他内容是正确的,但我认为您需要

HAVING SUM(ss.maxscore)>50

答案 1 :(得分:1)

如果您在分数的50以上寻找真假,那么您可以使用:

ORDER BY score, totalscore>50

虽然只是在订单中添加totalscore更有意义。

如果您尝试仅使用totalscore> 50然后在不知道你的表格的情况下,最简单的事情是添加另一个子选择

SELECT * FROM 
    ( SELECT h.hacker_id,h.name,SUM(ss.maxscore) totalscore 
      from (
        SELECT hacker_id,challenge_id,MAX(score) maxscore 
        FROM submission_table s 
        GROUP BY challenge_id,hacker_id
      ) as ss 
      JOIN hacker_table h 
      ON ss.hacker_id = h.hacker_id 
      GROUP BY h.hacker_id 
    )
    Where totalscore > 50
    Order by totalscore Desc,hacker_id ASC