mysql子字符串重复相同的值
你能帮我做这个吗? 我对使用substring_index分割列中的值有问题,我对前两个值1/3不感兴趣 表名=cc 路径(下面的示例值) 183年1月3日mysql子字符串重复相同的值,mysql,sql,substring,mysql-workbench,Mysql,Sql,Substring,Mysql Workbench,你能帮我做这个吗? 我对使用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
`
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
`
它给出了输出
183年1月3日
. 我不希望183重复,我更喜欢空白或0,因为它在正确的索引中找不到
您可以使用
案例
或。也许更简单,只需在列中添加一组斜杠:
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;
这将返回一个空字符串('
),而不是0
或NULL
。如果您想要一个数字,那么我只需在字段上执行+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;