MySql : Convert Column data to row

删除回忆录丶 提交于 2019-12-11 16:59:33

问题


I have a table in mysql which looks like below.

id  cust_id date    data

1   1   1/1/2018    a b c d e f g

2   1   2/1/2018    h I j k l m n 

Here in this example data column is having huge data seperated by space like a b c d, I would like to show case as in row like below

id  cust_id date    data

1   1   1/1/2018    a

1   1   1/1/2018    b

1   1   1/1/2018    c

1   1   1/1/2018    d

2   2   2/1/2018    h

2   2   2/1/2018    i

2   2   2/1/2018    j

2   2   2/1/2018    k

I have checked few option like using unpivot function, but unable to achieve my output. Thanks in advance !!


回答1:


select
  tablename.id,
  tablename.date
  ,SUBSTRING_INDEX(SUBSTRING_INDEX(tablename.data, ' ', numbers.n), ' ', -1) name
from
  (
    SELECT @row := @row + 1 as n FROM 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t1,
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2,
    (SELECT @row:=0) r
  ) numbers INNER JOIN Table1 tablename
  on CHAR_LENGTH(tablename.data)
     -CHAR_LENGTH(REPLACE(tablename.data, ' ', ''))>=numbers.n-1
order by
  id, n

Check link for output

http://sqlfiddle.com/#!9/fa0dcb/1

EXPLANATION: First go through the inner query i.e.

select 0 
union all 
select 1 
union all 
select 3 
union all 
select 4 
union all 
select 5 
union all 
select 6 
union all 
select 6 
union all 
select 7 
union all 
select 8 
union all 
select 9

This will generate a table of 10 rows with 10 numbers.

Now the other query :

 SELECT @row := @row + 1 as n FROM 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t1

Since above query is generating row numbers from below table 't' and table 't1' which is separated by ',' means that they are producing Cartesian product of their total rows. For example: t have 10 rows and t1 also have 10 rows so, there Cartesian product produces 100 rows. So @row variable incremented 100 times and gives 100 rows of 100 numbers from 1 to 100.

The below query:

SUBSTRING_INDEX(SUBSTRING_INDEX(tablename.data, ' ', numbers.n), ' ', -1)

this one will take "a b c d e f g h" one by one.

For example: take numbers.n = 1 then inner substring_index will find index of first space and will return string before that index i.e. 'a' and then outer substring_index will find the space from the end of the resulting string and will give the last character from the string i.e. 'a'.

Now if you take numbers.n = 2 then inner substring_index will find index of first space and will return string before that index i.e. 'a b' and then outer substring_index will find the space from the end of the resulting string and will give the last character from the string i.e. 'b'

Always try to breakdown the query like this and you will able to understand the query in simpler way.



来源:https://stackoverflow.com/questions/49274506/mysql-convert-column-data-to-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!