Split Comma Separated String in My SQL
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `String_Split`(
vString VARCHAR(8000),
vSeparator VARCHAR(5)
)
BEGIN
DECLARE vDone tinyint(1) DEFAULT 1;
DECLARE vIndex INT DEFAULT 1;
DECLARE vSubString VARCHAR(15);
DROP TABLE IF EXISTS tmpIDList;
CREATE TEMPORARY TABLE tmpIDList (ID INT);
WHILE vDone > 0
DO
SET vSubString = SUBSTRING(vString, vIndex,
IF(LOCATE(vSeparator, vString, vIndex) > 0,
LOCATE(vSeparator, vString, vIndex) - vIndex,
LENGTH(vString)
));
IF LENGTH(vSubString) > 0 THEN
SET vIndex = vIndex + LENGTH(vSubString) + 1;
INSERT INTO tmpIDList VALUES (vSubString);
ELSE
SET vDone = 0;
END IF;
END WHILE;
END
you can call this for every string passed by fronend and use this any sql query ,After that you should drop the temp table
like : call String_Split('1,1,1,12,3,34,22',',');
select mem_name from member where Id in (select id from tmpIDList )
DROP table tmpIDList;
Comments
Post a Comment