中易网

mysql case when 碰上中文字符串

答案:2  悬赏:20  
解决时间 2021-02-17 17:56
碰上中文字符串

大家好
我有一个表如下
publications id | publications field id| publications description
1000 | 1 | 小名
1000 | 2 | 2014
1000 | 4 | aa@...
1000 | 6 | 123-456-789
1012 | 1 | 阿花

我想把它结合成这样
publications id | publications description
1000 |小名,2014,NULL,aa@..,NULL,123-456-789
1012 |阿花, NULL, NULL, NULL, NULL, NULL

目前是使用casewhen
SELECt publications id,
case publications field id when ‘1’ then publications description else 'null'end as column1,
case publications field id when ‘2’ then publications description else 'null'end as column2,
case publications field id when ‘3’ then publications description else 'null'end as column3,
case publications field id when ‘4’ then publications description else 'null'end as column4,
case publications field id when ‘5’ then publications description else 'null'end as column5,
case publications field id when ‘6’ then publications description else 'null'end as column6
FROM publications
Group by publications id

结果却产出
publications id | publications description
1000 | NULL, NULL, NULL, NULL, NULL, NULL
1012 |阿花, NULL, NULL, NULL, NULL, NULL

竟然只有出现一次阿花,其他都不见了??
到底是哪里写错呢?是因为中文吗?还是>
最佳答案
  • 二级知识专家网友:山鬼偶尔也合群
  • 2021-02-17 15:00
恩。。。感觉不大对呢。
小名,2014,NULL,aa@..,NULL,123-456-789
是在一个字段里吧,按照你下面的写法,好像是放到了很多的字段里,你确定这个是你要的?
你用group_concat函数试试,应该能达到你上面的需求。(就是没有null)
至于你上面的问题,你的语句去掉group查出来的结果应该是
1000 小名 null null null null null
1000 null 2014 null null null null
。。。。。。。。。
你在group聚合的时候,没有指令上面的字段和下面的字段用什么方式结合,就是说“小名+null”用什么方式结合,也就是说在case when的外层少了一个聚合函数,不过按照这个写法,这个聚合函数是什么我还真不清楚,因为不管怎么聚和每个字段都会出现一堆的NULL,跟你的要求不符。
比较简单的解决办法,个人建议补齐1-6,没有就写null,这样查询的话用group_concat就行。
如果不加,个人感觉就比较麻烦了。
先用null合并,就是不加单引号的null,聚合后在显示的时候用上ifnull
全部回答
  • 1楼网友:滚刀废物浮浪人
  • 2021-02-17 15:25
我不会~~~但还是要微笑~~~:)
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯