我需要将名为TEAM的列中的值从一行复制到另一行。两行都需要具有相同的团队名称。这是我的查询不起作用:
$query = "UPDATE profiles SET team = (SELECT team FROM profiles WHERE id = '$coach_id') WHERE id = '$player_id'";
我尝试删除单引号,删除“FROM profiles”,将值更改为table.value,尝试提供newdata.clan别名,我甚至尝试将值更改为整数而不是参数。什么都行不通,这就是我得到的:
错误:您的SQL中有错误 句法;检查手册 对应于您的MySQL服务器 用于正确语法的版本 靠近'WHERE id ='')WHERE id ='''at 第3行
答案 0 :(得分: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 :(得分:1)
此外,您应该用花括号括起PHP变量:
$query = "UPDATE profiles SET team = \"(SELECT team FROM profiles WHERE id = '{$coach_id}')\" WHERE id = '{$player_id}'";
答案 2 :(得分:1)
来自MySQL手册:
“目前,您无法更新表格 并从a中的同一个表中选择 子查询“。
来源: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
}
?>