Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

php - Want to Split string in mysql using SUBSTRING_INDEX

I need to assign AAA-BBB-CCC in 3 different MySQL variables.

Here's the unsuccessful code:

DELIMITER $$
USE `testdepot`$$
DROP PROCEDURE IF EXISTS `sp_test_dlr`$$
CREATE PROCEDURE `sp_test_dlr`(IN id VARCHAR(250),OUT temp1 VARCHAR(50)
,OUT temp2 VARCHAR(50),OUT temp3 VARCHAR(50))
BEGIN
DECLARE t1 VARCHAR(100);
DECLARE t2 VARCHAR(100);
DECLARE t3 VARCHAR(255);
SET @t1 = SUBSTRING_INDEX(id, '-', 1);
SET @t2 = SUBSTRING_INDEX(id, '-', -1);
SET @t3 = SUBSTRING_INDEX(id, '-', -1);
SELECT @t1 INTO temp1;
SELECT @t2 INTO temp2;
SELECT @t3 INTO temp3;
END$$
DELIMITER ;

How can I separate above AAA-BBB-CCC string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SET @t3 = SUBSTRING_INDEX(SUBSTRING_INDEX(id, '-', -2), '-', 1);

There are other ways to do it, but I find this one quite amusing :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...