简体   繁体   English

MySQL:如果在Select中使用IN()语句? 我该如何查询?

[英]MySQL: If IN() statement within Select? How do I query this?

How do I write the query like: 我如何像这样写查询:

SELECT userid, 
 (IF field IN(1,2,3,900,658,4,5,6,9,10,11,12,13,10025,14,15,16,17,18,19) score = score +20, ELSE IF field IN(23,45,67,777,53,54) score = score + 40,
  ELSE IF field IN(120,332,563,334,634,423,333) score = score + 60) AS score 
 WHERE field IN(1,2,3,900,658,4,5,6,9,10,11,12,13,10025,14,15,16,17,18,19,23,45,67,777,53,54,120,332,563,334,634,423,333) 
GROUP BY userid ORDER BY score DESC

I'm trying to find all users who have a field set to one of the given numbers. 我正在尝试查找将字段设置为给定数字之一的所有用户。 Certain numbers have different scores (20 points, 40 points and 60 points), so I need to add up the scores to get a total score for each userid and then rank users by the total score. 某些数字具有不同的分数(20分,40分和60分),因此我需要将这些分数相加以获得每个用户ID的总得分,然后按总得分对用户进行排名。

You might be looking for this: 您可能正在寻找:

SELECT   userid, 
         sum(
             CASE
                WHEN field IN (1,2,3,4,5,6,9,10,11,12,13,14,15,16,17,18,19) THEN 20 
                WHEN field IN (23,45,67,53,54) THEN 40
                ELSE 60 
             END) AS score
FROM     mytable 
WHERE    field IN (1,2,3,4,5,6,9,10,11,12,13,14,15,16,17,18,19,23,45,67,53,54,120,
                   332,563,334,634,423,333)
GROUP BY userid
ORDER BY score DESC

You missed th from Table and you can do with case 您错过了Table,可以解决问题

select UserId,
     case 
       when field < 20 then  Score + 20
       when field IN(23,45,67,53,54) then  Score + 40
       else  score + 60
      end as Score
FROM mytable
where field IN(1,2,3,4,5,6,9,10,11,12,13,14,15,16,17,18,19,23,45,67,53,54,120,332,563,334,634,423,333) 
group by UserId
order by Score desc 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM