mysql 根据固定字符分割字符串
时间: 2023-08-31 12:11:04 浏览: 110
mysql分割字符串
4星 · 用户满意度95%
根据固定字符分割字符串可以使用MySQL的字符串分割函数substring_index。该函数的语法为substring_index(str, delim, count),其中str是要处理的字符串,delim是分隔符,count是计数。如果count是正数,函数会从左往右数,返回第N个分隔符左边的全部内容;如果count是负数,函数会从右往左数,返回第N个分隔符右边的所有内容。例如,如果要根据竖线字符"|"分割字符串"1|2|3|4|5",可以使用substring_index函数如下:
SELECT substring_index("1|2|3|4|5", "|", 1) AS split1,
substring_index(substring_index("1|2|3|4|5", "|", 2), "|", -1) AS split2,
substring_index(substring_index("1|2|3|4|5", "|", 3), "|", -1) AS split3,
substring_index(substring_index("1|2|3|4|5", "|", 4), "|", -1) AS split4,
substring_index(substring_index("1|2|3|4|5", "|", 5), "|", -1) AS split5;
这样就可以将字符串"1|2|3|4|5"根据竖线字符分割成五个列展示,分别为split1、split2、split3、split4、split5。每个列对应字符串中的一个层级id。
#### 引用[.reference_title]
- *1* *3* [mysql字符串分割多列展示](https://blog.csdn.net/m0_38001814/article/details/122576984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [MySQL分割字符串的实现,mysql 字符串分割函数](https://blog.csdn.net/weixin_36314729/article/details/113301938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文