MySQL:由分隔符字符串拆分的字符串的左侧部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5734504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL : left part of a string split by a separator string?
提问by Dylan
I need a MySQL function to get the left part of a string with variable length, before the separator.
我需要一个 MySQL 函数来获取长度可变的字符串的左侧部分,在分隔符之前。
For example, with separator string '==' :
例如,使用分隔符字符串 '==' :
abcdef==12345 should return abcdef
abcdefgh==12 should return abcdefgh
Also the same thing, but for the right part...
同样的事情,但对于正确的部分......
回答by Shakti Singh
SELECT SUBSTRING_INDEX(column_name, '==', 1) FROM table ; // for left
SELECT SUBSTRING_INDEX(column_name, '==', -1) FROM table; // for right
回答by Nicola Cossu
select substring_index('abcdef==12345','==',1)
for the right part use -1 instead of 1.
对于正确的部分,使用 -1 而不是 1。
回答by Brandon
I would look into the substring function in SQL which is SUBSTR, but it is more for set positions in the string, not so much for variable lengths.
我会研究 SQL 中的 substring 函数,它是 SUBSTR,但它更多地用于字符串中的设置位置,而不是可变长度。