0

这是我的查询:

$student_id = 34;
$user_id = 1;
$lesson_id = 25;

$query = ("UPDATE lessons SET 
assigned='1',
student_id='$student_id'
WHERE user_id='$user_id'
AND lesson_id='$lesson_id'")

我正在尝试运行 UPDATE 以仅影响一行。我有一张这样的桌子:

_____________________________________
user_id|lesson_id|assigned|student_id
_______|_________|________|__________
1      |16       |1       |12
_______|_________|________|__________
1      |25       |0       |0
_______|_________|________|__________
1      |25       |0       |0
_______|_________|________|__________
1      |7        |1       |34
_______|_________|________|__________
1      |13       |1       |12
_______|_________|________|__________
1      |13       |0       |0
_______|_________|________|__________

当我执行这个 UPDATE 时,我发现所有匹配的行都被更新了。因此,如果我尝试根据课程 ID 25 进行更新,两行都会更改。每个查询我只需要更改一个。

我尝试将 LIMIT 1 添加到查询中,但它只允许我执行一次。我返回表单并再次将所有信息提交给查询,但它没有第二次执行。我觉得这很奇怪。

那么,我错过了什么?

4

2 回答 2

1

尝试使用:

$query = ("UPDATE lessons SET 
    assigned='1',
    student_id='$student_id'
    WHERE user_id='$user_id'
    AND lesson_id='$lesson_id'
    AND assigned ='0' LIMIT 1");
于 2013-07-09T13:13:51.710 回答
0

This question does not make much sense. You have a table which has two exactly the same rows and your condition is to update these rows. So how do you want to limit the update?

You should add some unique column to identify records. Now all what you can do is LIMIT statement.

Your Where statement affects these rows:

user_id|lesson_id|assigned|student_id
_______|_________|________|__________
1      |25       |0       |0
_______|_________|________|__________
1      |25       |0       |0
_______|_________|________|__________

You should really think about redesigning your database. However, if it's not possible then use assigned field in WHERE condition. WHERE assigned <> 1

于 2013-07-09T13:11:42.777 回答