在PostgreSql中选择For Update语句

时间:2022-12-10 14:23:22

I want to select fields in a recor of a table and update just one of these fields. How can I do?

我想在表的记录中选择字段并仅更新其中一个字段。我能怎么做?

I try this:

我试试这个:

SELECT v.idvideo, v.title 
FROM video v  WHERE v.schedulingflag IS FALSE AND v.errorflag IS FALSE 
ORDER BY v.idvideo  LIMIT 1 FOR UPDATE ;

UPDATE video  SET schedulingflag = true;

But in this way it sets field "schedulingflag" true in all record!

但通过这种方式,它在所有记录中设置字段“schedulingflag”为真!

1 个解决方案

#1


12  

The SELECT FOR UPDATE syntax tells PG that you're going to be updating those records and locks them against concurrent access. However you still need to issue the appropriate UPDATE call to change the particular records you've locked.

SELECT FOR UPDATE语法告诉PG您将更新这些记录并锁定它们以防止并发访问。但是,您仍需要发出相应的UPDATE调用来更改已锁定的特定记录。

In this case, just use the same WHERE clause in your UPDATE, e.g:

在这种情况下,只需在UPDATE中使用相同的WHERE子句,例如:

UPDATE video  SET schedulingflag = true 
WHERE schedulingflag IS FALSE AND errorflag IS FALSE;

#1


12  

The SELECT FOR UPDATE syntax tells PG that you're going to be updating those records and locks them against concurrent access. However you still need to issue the appropriate UPDATE call to change the particular records you've locked.

SELECT FOR UPDATE语法告诉PG您将更新这些记录并锁定它们以防止并发访问。但是,您仍需要发出相应的UPDATE调用来更改已锁定的特定记录。

In this case, just use the same WHERE clause in your UPDATE, e.g:

在这种情况下,只需在UPDATE中使用相同的WHERE子句,例如:

UPDATE video  SET schedulingflag = true 
WHERE schedulingflag IS FALSE AND errorflag IS FALSE;