我有下面的数据
ID name page1 page2 page3 page4 page5
1 demo 1 1 1 1 1
10 compliance 1 1 0 0 0
12 compliance 1 1 1 0 0
20 compliance 0 1 0 1 0
25 compliance 1 1 0 0 1我正在尝试将包含1的列合并到其中。
我用Concat试过,如下所示
Select id, name,
Concat (
case when page1 = 1 then 'page1, ' End
case when page2 = 1 then 'page2, ' End
case when page3 = 1 then 'page3, ' End
case when page4 = 1 then 'page4, ' End
case when page5 = 1 then 'page5, ' End
)但这给了我下面的机会
ID name yes page
1 demo page1, page2, page3, page4, page5
10 compliance NULL
12 compliance NULL
20 compliance NULL
25 compliance NULL我希望有这样的东西
ID name yes page
1 demo page1, page2, page3, page4, page5,
10 compliance page1, page2,
12 compliance page1, page2, page3,
20 compliance page2, page3, page4,
25 compliance page1, page2, page5,请让我知道,如果我可以做我的凹声明工作,这样我就可以有像上面这样的桌子。非常感谢你的帮助!
发布于 2022-02-11 17:51:59
当您有一个没有匹配的CASE表达式时,表达式的结果是NULL。当您将NULL与任何其他字符串连接时,结果是NULL。这解释了你所看到的结果。
解决方案包括一个默认值,每个表达式如下:
case when page1 = 1 then 'page1, ' Else '' End记住,NULL并不意味着“空”。意思是“我不知道”。当你把和“我不知道”结合起来,结果总是会是“我不知道”。
发布于 2022-02-11 17:58:01
在MySql中,concat授予NULL值,任何与NULL连接的字符串都会导致NULL。
相反,使用忽略空值的concat_ws。呃,
Concat_ws (', '
case when page1 = 1 then 'page1' End,
case when page2 = 1 then 'page2' End...
)发布于 2022-02-11 17:58:54
https://stackoverflow.com/questions/71084417
复制相似问题