【问题标题】:mysql substring repeats same valuemysql子字符串重复相同的值
【发布时间】:2017-11-28 17:06:18
【问题描述】:

你能帮我解决这个问题吗? 我在使用 substring_index 拆分列中的值时遇到问题,我对前两个值 1/3 不感兴趣。

表名 = cc

路径(下面的示例值)

183 年 1 月 3 日

 `
select 
cc.path,
substring_index(substring_index( cc.path, '/',3),'/',-1)
,

substring_index(substring_index( cc.path, '/',4),'/',-1)
,

substring_index(substring_index( cc.path, '/',5),'/',-1)
from cc
`

给出输出

1/3/183 183 183 183 .我不希望 183 重复我更喜欢在正确索引中找不到时使用空白或 0。

【问题讨论】:

    标签: mysql sql substring mysql-workbench


    【解决方案1】:

    您可以使用 case 或 . . .也许更简单,只需在列中添加一堆斜线:

    select cc.path,
           substring_index(substring_index(concat(cc.path, '/////'), '/', 3), '/', -1),   
           substring_index(substring_index(concat(cc.path, '/////'), '/', 4), '/', -1),
           substring_index(substring_index(concat(cc.path, '/////'), '/', 5), '/', -1)
    from cc;
    

    这将返回一个空字符串 (''),而不是 0NULL。如果您想要一个数字,那么我只需在字段上执行+ 0 即可将值转换为数字。

    或者,如果你想要 0 作为字符串:

    select cc.path,
           substring_index(substring_index(concat(cc.path, '/0/0/0/0/0'), '/', 3), '/', -1),   
           substring_index(substring_index(concat(cc.path, '/0/0/0/0/0'), '/', 4), '/', -1),
           substring_index(substring_index(concat(cc.path, '/0/0/0/0/0'), '/', 5), '/', -1)
    from cc;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多