SQL语句:
SET @str = '7654,7698,7782,7788'; SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@str, ',', help_topic_id + 1), ',', - 1) AS num FROM mysql.help_topic WHERE help_topic_id < LENGTH(@str) - LENGTH(REPLACE (@str, ',', '')) + 1
封装成存储过程:
/* * 名称:按指定字符拆分字符串 * 输入参数,str,需拆分的字符串 * 输入参数,splitChar,用于分隔的字符 */ DROP PROCEDURE IF EXISTS proc_common_split; CREATE PROCEDURE proc_common_split ( -- 需拆分的字符串 str LONGTEXT, -- 用于分隔的字符 splitChar CHAR(1) ) BEGIN SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(str, splitChar, help_topic_id + 1), splitChar, - 1) AS Result FROM mysql.help_topic WHERE help_topic_id < LENGTH(str) - LENGTH(REPLACE (str, splitChar, '')) + 1; END; /* 输入参数,str,需拆分的字符串 输入参数,splitChar,用于分隔的字符 CALL proc_common_split('1,2,3', ','); */