Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 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 Reply

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...