php - Seperate row in column with SQL query -


i have row with

(name1, name2, name3, name4) 

and row with

(2, 3, 4, 5, 7) 

i need present data in 2 columns, name number in same place this:

names  |  numbers -------------------- name1     2 name2     3 name3     4 name4     5 

i tried subsrting_index repaet same number @ first

i think in mysql might done in way similar this:

create table t (names char(255), numbers char(255)); insert t(names, numbers) values('name1,name2,name3,name4', '2,3,5,7');  select substring_index(substring_index(t.names, ',', idx), ',', -1),        substring_index(substring_index(t.numbers, ',', idx), ',', -1) t, (select 1 idx union select 2 union select 3 union select 4) r 

here complete sqlfiddle

sure, subquery generating number sequence should adjusted case. there number of examples on stackoverflow how achieve particular task.

update:

here example how handle 10000 elements in row.

select substring_index(substring_index(t.names, ',', idx), ',', -1),        substring_index(substring_index(t.numbers, ',', idx), ',', -1) t, (select @row := @row + 1 idx (select 0 union select 1 union select 3 union select 4 union select 5 union select 6 union select 6 union select 7 union select 8 union select 9) n, (select 0 union select 1 union select 3 union select 4 union select 5 union select 6 union select 6 union select 7 union select 8 union select 9) n2, (select 0 union select 1 union select 3 union select 4 union select 5 union select 6 union select 6 union select 7 union select 8 union select 9) n3, (select 0 union select 1 union select 3 union select 4 union select 5 union select 6 union select 6 union select 7 union select 8 union select 9) n4, (select @row:=0) n5 @row < (select max(length(names) - length(replace(names, ',', '')) + 1) t))r 

notice reduce amount of operations on strings @ first find maximum amount of elements in 1 row using subquery.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -