带条件的SQL新表

带条件的SQL新表,sql,string,datatables,Sql,String,Datatables,我有一个表,我想通过sql将其解析为另一个表。问题在于存在一个条件: 旧版本中有3列 column_1 column_2 column_3 0 1 1 1 0 1 1 1 1 我想将它们存储到新表的列中,如: new_column_1 no/yes/yes yes/no/yes yes/yes/yes 提前感谢。您可以使用case表达式和字符串串联,如下所示: selec

我有一个表,我想通过sql将其解析为另一个表。问题在于存在一个条件: 旧版本中有3列

column_1 column_2 column_3
    0            1      1
    1            0      1
    1            1      1
我想将它们存储到新表的列中,如:

new_column_1

no/yes/yes
yes/no/yes
yes/yes/yes

提前感谢。

您可以使用
case
表达式和字符串串联,如下所示:

select t.*,
    (case when column_1 = 1 then 'yes' else 'no' end)
    || '/' || (case when column_2 = 1 then 'yes' else 'no' end)
    || '/' || (case when column_3 = 1 then 'yes' else 'no' end) as new_column_1
from mytable t
这使用标准字符串连接运算符
| |
;有些数据库有另一个操作符或函数

一些数据库还支持
concat_ws()
,这稍微简化了表达式:

select t.*,
    concat_ws('/', 
        (case when column_1 = 1 then 'yes' else 'no' end)
        (case when column_2 = 1 then 'yes' else 'no' end)
        (case when column_3 = 1 then 'yes' else 'no' end)
    ) as new_column_1
from mytable t

您可以使用
insert。。。选择
syntax,尽管我不建议存储这些派生信息:相反,您可以创建一个视图,或者在原始表中添加计算列。

这是一个非常糟糕的主意。这只会给你带来很多麻烦。谢谢你的快速回复。我还有一个问题,如果我们用else=''替换else='0',我们将得到yes/no/yes或yes/yes/或yes/。如何删除空行后面的“/”?