mysql用逗号分割字符串

这是我的表结构,需要分割的就是KEY的值

采用的是mysql的substring_index函数:

  1. select
        a.ID,a.DOCTORID,
            substring_index(substring_index(a.`KEY`,',',b.help_topic_id+1),',',-1)  as key1
        from
            doctorarea a
        join
            mysql.help_topic b  on b.help_topic_id < (length(a.`KEY`) -     length(replace(a.`KEY`,',',''))+1)
    
  2. 我在我本地mysql数据库运行没有任何问题,但是放在项目中,调用远程数据库,结果永远是空。原因是sql里面用了mysql一个自带的临时表:help_topic,我本地里面是该表是有值的,但是发现远程数据库中该表是没有值,我把我表里面的数据,复制到远程数据库的 help_topic中再次运行,发现可以了。
  3. help_topic:一般为系统表,轻易不要修改,那么可以自己新建一个表,help_index,里面就一个字段help_index_id,只要保证这个表里面有较多数据,就可以了:那么上面代码可以换成下面这个:
select a.ID,a.DOCTORID, substring_index(substring_index(a.`KEY`,',',b.help_topic_id+1),',',-1) as key1 from doctorarea a join help_index b on b.help_index_id < (length(a.`KEY`) - length(replace(a.`KEY`,',',''))+1)

猜你喜欢

转载自blog.csdn.net/qq_21036901/article/details/80998757