I have a column and I want to create a query to have multiple columns.
For example, I have test column.
The data of this column looks like this:
record1 => **option1|value1;option2|value2**
record2 => **option3|value3**
record3 => **option3|value3;option1|value1;option2|value2;option4|value4**
The number of options and values are undefined.
I want to have this one test column break into number of option like this :
option1 option2 option3 option4
'value1' 'value2' 'value3'
'value3'
'value1' 'value2' 'value3' 'value4'
my table like this :
id name options
1 name age|12;color|red;active|yes;car|no
2 name age|15;member|yes
Thanks!
You can write a custom MySQL function called user defined function that can receive the variable name and the value to search for and then return the row or ID etc. as you need.
In that function, you will need to loop through all the rows in a read only cursor and use SUBSTRING_INDEX() to get the values separately and check for the match.
If you need further assistance then let me know.
For more info look into the MySQL manual for string functions here.
An extract from a user comment from the above link that might help you write the function:
This will split an IP address (“a.b.c.d”) into 4 respective octets: