MYSQL CASE 语句多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37460225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:50:25  来源:igfitidea点击:

MYSQL CASE STATEMENT MULTIPLE CONDITIONS

mysqlsqldatabase

提问by Lavanya D.

Please help to check the error in following sql code:

请帮助检查以下sql代码中的错误:

select
case when (z.mso_group_id = '3' and z.model_id = '22887081') then coalesce(level_2_id,996) level_2_id
      when (z.mso_group_id = '4' and z.model_id = '22911859') then coalesce(level_2_id,997) level_2_id
      when (z.mso_group_id = '5' and z.model_id = '22915074') then coalesce(level_2_id,998) level_2_id
      when (z.mso_group_id = '2' and z.model_id = '22908275') then coalesce(level_2_id,999) level_2_id
  end level_2_id
from
database_name

回答by Gordon Linoff

You are repeating the column alias. Do you intend this?

您正在重复列别名。你有这个打算吗?

select (case when (z.mso_group_id = '3' and z.model_id = '22887081') then coalesce(level_2_id,996)
             when (z.mso_group_id = '4' and z.model_id = '22911859') then coalesce(level_2_id,997)
             when (z.mso_group_id = '5' and z.model_id = '22915074') then coalesce(level_2_id,998)
             when (z.mso_group_id = '2' and z.model_id = '22908275') then coalesce(level_2_id,999)
        end) as level_2_id
from database_name;