0

I have a problem with comma separated values when fetch and insert to 1 table from to another table,

my problem is,

I am using PHP, Mysql

table_1 has comma separated values like this

1 | apple1, apple2, apple3 
2 | samsung1,samsung2, samsung3, samsung4
3 | nokia1,nokia2

I want to fetch these values from above table and inset to another table with same id, like this
table_2
1 | 1 | apple1
2 | 1 | apple2
3 | 1 | apple3
4 | 2 | samsung1
5 | 2 | samsung2
6 | 2 | samsung3
7 | 2 | samsung4
8 | 3 | nokia1
9 | 3 | nokia2

please anyone can help me to resolve this problem I have a big comma separated table to insert with this,

thanks!

4

2 に答える 2

0

たとえば、この値を一度に取得できます..apple1、apple2、apple3、変数にそれらが含まれると思います。たとえば、それは$container 文字列なので、最初に配列にします

$arr=(explode(",", $container));

今ここにPHPのforeachループのアプローチがあります.$containerごとに一意にIDを取得している場合は、次のようにします

$id //your id container variable

挿入クエリを次のように適用します。

foreach($arr as $val)
{
mysqli_query($con, "insert into table_name set r_id='$id',val='$val'");
}

一意の行ごとにカンマ区切りの文字列を挿入します...

于 2013-09-27T10:27:19.877 に答える
0

挿入して変換します。

load infile 

挿入に必要なものです

列を行に変換するには、select id, col1 from table union all id col2 from table union all id, col3 from table...

于 2013-09-27T10:05:25.867 に答える