0

Suppose I have a string....

$string = "This, Cat, Likes, Lasagna";

Using MYSQL only, would it be possible to get an array result like

"This" "Cat" "Likes" "Lasagna".

Basically, exactly like if I was to do

$result = explode('/',$string);

Just in MYSQL only? I've seen other examples, but they seem to only return the first result. I need all, in an array format.

Ideally, I'd like to avoid creating new MYSQL functions, and just run it as a independent query.

So far, I've got something like...but this doesn't quite work.

 SELECT SUBSTRING_INDEX( 'This, Cat, Likes, Lasagna', ',', 1 ) as part1,
 SUBSTRING_INDEX( 'This, Cat, Likes, Lasagna', ',', 2 ) as part2,
 SUBSTRING_INDEX( 'This, Cat, Likes, Lasagna', ',', 3 ) as part3,
 SUBSTRING_INDEX( 'This, Cat, Likes, Lasagna', ',', 4 ) as part4,
 SUBSTRING_INDEX( 'This, Cat, Likes, Lasagna', ',', 5 ) as part5

1 Answer 1

1

try this..

SET @field = 'This, Cat, Likes, Lasagna'; -- your field
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(@field, ',', -5),',',1) as part1,
SUBSTRING_INDEX(SUBSTRING_INDEX(@field, ',', -4),',',1) as part2,
SUBSTRING_INDEX(SUBSTRING_INDEX(@field, ',', -3),',',1) as part3,
SUBSTRING_INDEX(SUBSTRING_INDEX(@field, ',', -2),',',1) as part4,
SUBSTRING_INDEX(@field, ',', -1) as part5;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I ended up coming to a very similar conclusion myself. But, thank you, thank you, thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.