在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']));
我对此非常陌生,我做错了什么?
答案 0 :(得分:1)
提供了这些工作
items
和accounts
表你可以尝试
$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']));