JOIN UPDATE mysql,无法让它工作

时间:2011-12-30 20:15:58

标签: mysql join

在mysql中更新时如何使用连接?到目前为止,我有这个......

$q = $dbc -> prepare("UPDATE items i JOIN accounts a SET i.shrapnel = i.shrapnel-1000, a.gender = a.? WHERE id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

我对此非常陌生,我做错了什么?

3 个答案:

答案 0 :(得分:1)

提供了这些工作

  • 如果id字段同时位于itemsaccounts
  • id与两个表中的数据类型相同

你可以尝试

$q = $dbc -> prepare("UPDATE items i JOIN accounts a USING (id) SET i.shrapnel = i.shrapnel-1000, a.gender = a.? WHERE i.id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

$q = $dbc -> prepare("UPDATE items i JOIN accounts a ON i.id = a.id SET i.shrapnel = i.shrapnel-1000, a.gender = a.? WHERE i.id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

$q = $dbc -> prepare("UPDATE items i NATURAL JOIN accounts a SET i.shrapnel = i.shrapnel-1000, a.gender = a.? WHERE i.id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

答案 1 :(得分:0)

id需要一个表标识符。我也会用“AS”。

$q = $dbc -> prepare("UPDATE items AS i JOIN accounts AS a SET i.shrapnel = i.shrapnel-1000, a.gender = a.? WHERE a.id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

答案 2 :(得分:0)

您可能需要加入开启某些内容吗?:

$q = $dbc -> prepare("
UPDATE 
  items i JOIN accounts a ON (i.commonField = a.commonField) 
SET 
  i.shrapnel = i.shrapnel-1000 , 
  a.gender = a.? 
WHERE 
  id = ? LIMIT 1");
$q -> execute(array($gender, $user['id']));

检查:http://dev.mysql.com/doc/refman/5.0/en/join.html