2012-03-13 80 views
0

MySQL數據傳輸我有這個表synonym_temp:與重新格式化

id  | synonyms 
---------------------------- 
1  | ebay,online,dragon 
2  | auto, med 

我想將其傳送到同義詞表,但每個同義詞應分別插入。 Like:

id  | synonym 
---------------------------- 
1  | ebay 
1  | online 
1  | dragon 
2  | auto 
2  | med 

我應該怎麼做?提前致謝。

回答

1

使用PHP

$records=mysql_query("select * from synonm_temp"); 

foreach($records as $row) 
{ 
    $synonums=$row['synonyms']; 
    $synonums_array=explode(',', $synonums); 
    $id=$row['id']; 
    foreach($synonums_array as $syn) 
    { 
     mysql_query("insert into synonm_temp values ($id,'$syn') "); 
    } 

} 
1

您可以用數字或帳簿桌包含整數的順序列表做到這一點:

Select T.id, Substring(T.synonym, N.Value, Locate(', ', T.synonym+ ', ', N.Value) - N.Value) 
From Numbers As N 
    Cross Join MyTable As T 
Where N.Value <= Len(T.synonym) 
    And Substring(', ' + T.synonym, N.Value, 1) = ', ' 

在上述情況下,我的Numbers表的結構,像這樣:

Create Table Numbers(Value int not null primary key)