MySQL:从一条记录到多条记录,有可能吗?

MySQL:从一条记录到多条记录,有可能吗?,mysql,sql,Mysql,Sql,我有一张这样的桌子: code allValues -------------- C001 1,2,3,4 C002 3,4,5 C003 6,7 code Value ---------- C001 1 C001 2 C001 3 C001 4 C002 3 C002 4 C002 5 C003 6 C003 7 我需要得到这样一张表: code allValues -------------- C001 1,2,3,4 C002 3,4,5 C003 6,7 code Value --

我有一张这样的桌子:

code allValues
--------------
C001 1,2,3,4
C002 3,4,5
C003 6,7
code Value
----------
C001 1
C001 2
C001 3
C001 4
C002 3
C002 4
C002 5
C003 6
C003 7
我需要得到这样一张表:

code allValues
--------------
C001 1,2,3,4
C002 3,4,5
C003 6,7
code Value
----------
C001 1
C001 2
C001 3
C001 4
C002 3
C002 4
C002 5
C003 6
C003 7
我可以用SQL语句吗?
我在PHPMyAdmin中使用MySQL,不知道是哪个特定的数据库引擎。

如果每个allvalue都有一个查找表,那么可以执行以下操作:

select t.code, lu.value
from t join
     lookup lu
     on find_in_set(lu.value, t.allvalues) > 0;
另一种方法(如果您知道AllValue的最大长度)是执行字符串操作:

select t.code,
       substring_index(substring_index(t.allvalues, ',', n.n), ',', -1) as vaue
from t cross join
     (select 1 as n union all select 2 union all select 3 union all select 4
     ) n
     on n.n <= length(t.allvalues) - length(replace(t.allvalues, ',', ''));

此版本假设最大长度为4,如问题所示。

首先拆分逗号分隔的值并存储在表中,然后将该表与主表连接。您可以使用php拆分值或在mysql中编写存储过程来执行相同的操作。所以,答案是肯定的,这是可能的。有可能有一个存储过程的草稿吗?@Sandip Patel,我只能使用sql语句,不能使用php。我从标记列表中删除了php。我们不做请求。展示你所拥有的,我们会帮助你。或者,你也可以这样搜索。这个问题已经回答了好几次了。