Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用更新查询复制列数据_Php_Mysql_Select - Fatal编程技术网

Php 使用更新查询复制列数据

Php 使用更新查询复制列数据,php,mysql,select,Php,Mysql,Select,我需要将名为TEAM的列中的值从一行复制到另一行。两行需要具有相同的团队名称。这是我的查询,不起作用: $query = "UPDATE profiles SET team = (SELECT team FROM profiles WHERE id = '$coach_id') WHERE id = '$player_id'"; 我尝试过删除单引号,删除“从配置文件”,将值更改为table.value,尝试过给出newdata.clan别名,甚至尝试过将值更改为整数而不是参数。什么都不管用,这

我需要将名为TEAM的列中的值从一行复制到另一行。两行需要具有相同的团队名称。这是我的查询,不起作用:

$query = "UPDATE profiles SET team = (SELECT team FROM profiles WHERE id = '$coach_id') WHERE id = '$player_id'";
我尝试过删除单引号,删除“从配置文件”,将值更改为table.value,尝试过给出newdata.clan别名,甚至尝试过将值更改为整数而不是参数。什么都不管用,这就是我得到的:

错误:您的SQL中有一个错误 句法;检查手册 对应于您的MySQL服务器 要使用的正确语法的版本 靠近'WHERE id=''的位置 第3行


此外,还应将PHP变量用大括号括起来:

$query = "UPDATE profiles SET team = \"(SELECT team FROM profiles WHERE id = '{$coach_id}')\" WHERE id = '{$player_id}'";
从MySQL手册:

“当前,您无法更新表 并从中的同一个表中进行选择 子查询。“

资料来源:

使用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
}
?>

必须像FinalForm编写的那样运行两个查询。我现在正在写一个完整的代码,你会看到的。谢谢FinalForm,他是第一个写代码的人,我只是输入了两个步骤来澄清。接受他的答案为正确答案。不,你只能选择一个,删除我的并添加最终表单:)我在上面..创建一个帐户!当我得到15个代表积分时,这一点就是你的了(:
<?
$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
}
?>