2011-06-23 8 views
2

TEAM列の値をある行から別の行にコピーする必要があります。両方の行に同じチーム名が必要です。これは動作しないというのが私のクエリです:私は、単一引用符を削除し、「プロファイルの中から」削除しようとしている更新クエリを使用した列データのコピー

$query = "UPDATE profiles SET team = (SELECT team FROM profiles WHERE id = '$coach_id') WHERE id = '$player_id'"; 

、table.valueする値を変更し、newdata.clan別名を与えることを試みたが、私も持っていますパラメータの代わりに値を整数に変更しようとしました。何も機能していない、これは私が得るものです:また

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = '') WHERE id = ''' at line 3

答えて

1
$query1 = "SELECT team FROM profiles WHERE id = '$coach_id'"; 

/* get the value of the first query and assign it to a variable like $team_name */ 

$query2 = "UPDATE profiles SET team = '$team_name' WHERE id = '$player_id'"; 
1

、あなたは中括弧でPHPの変数を囲む必要があります。

$query = "UPDATE profiles SET team = \"(SELECT team FROM profiles WHERE id = '{$coach_id}')\" WHERE id = '{$player_id}'"; 
1

をMySQLのマニュアルから:

"Currently, you cannot update a table and select from the same table in a subquery."

出典:http://dev.mysql.com/doc/refman/5.0/en/update.html

FinalFormが書いた方法を使用してください:

<? 
$coach_id = 2; 
$player_id = 1; 

$query1 = "SELECT team FROM profiles WHERE id = '$coach_id'"; 
$rs = mysql_query($query1); 
if ($row = mysql_fetch_array($rs)) { 
    $team_name = $row['team']; 
    $query2 = "UPDATE profiles SET team = '$team_name' WHERE id = '$player_id'"; 
    mysql_query($query2); 
    // Done, updated if there is an id = 1 
} else { 
    // No id with id = 2 
} 
?> 
+0

これは無駄な労力ですか? – Jessica

+0

FinalFormのように2つのクエリを実行する必要があります。私は今すぐ完全なコードを書いていますので、あなたが見るでしょう。 – Mattis

+0

ありがとうMattis。それは完全に動作します。私は興奮しています( – Jessica

関連する問題