我正在尝试同时更新两个表。该操作已成功完成。但是,它将更新表中的所有记录,而不是指定的记录。我已经尝试了建议here,但无济于事。任何想法可能是什么问题?我的代码如下所示。

$sql = "UPDATE $receiverTable, $currUserTable
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')";

最佳答案

您对JOIN没有任何条件(您真的不应该再用逗号编写JOIN了),因此每一行都被JOIN到其他每一行,这意味着它们都对一个行具有JOIN,其中一个WHERE条件为真,因此它们都将被更新。用适当的条件重写您的JOIN,问题将消失。就像是:

UPDATE $receiverTable
JOIN $currUserTable ON $currUserTable.somecolumn = $receiverTable.somecolumn
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')


如果无法联接表,则需要将UPDATE编写为两个单独的查询。

10-06 03:41