0

I'm trying to substring a mysql query result to get my seperate data columns. My delimiter is the character "||" that will separate each result. I'm searching online and can't seem to find a source that will substring a mysql result. I always find that it substring the columns from the table. Is this possible using substring?

I've created 2 tables which is number and sample:

Table sample

enter image description here

Table number

enter image description here

Here's my query:

SELECT sample.id,
       SUBSTRING_INDEX(SUBSTRING_INDEX(sample.DESCRIPTION, '::', numbers.n), '::', -1) AS DETAIL
FROM numbers
INNER JOIN sample ON CHAR_LENGTH(sample.DESCRIPTION) -CHAR_LENGTH(REPLACE(sample.DESCRIPTION, '::', ''))>=numbers.n-1
ORDER BY id,
         n

Here's my result query

enter image description here

Here's my expected result

enter image description here

Update

I've tried to do this in PHP and I've made another progress. However, I'm getting an undefined offset.

Code:

$querydata = "SELECT DESCRIPTION from sample";
$resultdata = $conn->prepare($querydata);
$resultdata->execute();

$storeresult = $resultdata->fetchObject();
$resultquery = $storeresult->DESCRIPTION;


$pieces = explode("::",$resultquery);

$countVal = count($pieces);

$counter = 0;
$counter1 = 1;
$counter2 = 0;


while($counter<$countVal){
$pieces1 = explode("||", $pieces[$counter]);
$pieces2 = explode("||", $pieces[$counter]);
$pieces3 = explode("||", $pieces[$counter2]);


echo $pieces1[0]."<br>"; // detail
echo $pieces2[1]."<br>"; // response


$counter++;
$rowCount++;

}
Rukikun
  • 271
  • 3
  • 19

0 Answers0