【问题标题】:Select Custom Column based on If Statement根据 If 语句选择自定义列
【发布时间】:2015-01-06 08:17:46
【问题描述】:

我有以下Survey

列是Survey_Name, Question, Response, Weight, QuestionType

除了显示此表中的值之外,我想要显示一个自定义列,该列的值取决于其他列。

例如,

让我们调用自定义列response_value 我希望此列显示weight 为具有QuestionType 为“评级”的行,而对于其他问题类型,我希望它显示Response,而不是单词,而是字段的值.

是的,weightresponsecolumns 已经在表中,但我希望它们根据该条件显示在自定义列中。

所以基本上是Select * from Survey, Select ([if statement here])

帮助?

【问题讨论】:

    标签: mysql sql tsql


    【解决方案1】:
    select *, case when questiontype = 'Rating' 
                   then cast(weight as signed)
                   else response
              end as response_value
    from survey
    

    【讨论】:

    • 是否可以将response_value的数据类型设置为nvarchar?因为我得到一个错误,该列是 int,并且一些响应有文本。
    • cast() 添加到int 值列。
    【解决方案2】:
    select *, if(questiontype = 'Rating',weight,response) as response_value from survey
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 2011-08-22
      • 2012-10-23
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多