从同一表 n Postgres 获取记录时更新列


Update a column while fetching record from the same table n Postgres

我在构建一个 API,我需要计算为用户帐户处理的请求数,所以每次从 API_USERS 表中执行 select 语句时,我想将同一表中的列(req_count)递增 1

所以我有该用户帐户发出的请求数,是否可以在获取记录时更新列?

要增加用户表中的计数器(如果我很好地理解您的问题):

-- It will add 1 to request count for the user <your user id>
UPDATE api_users
   SET api_users.req_count = api_users.req_count + 1
 WHERE api_users.id = <your user id>
   -- Add your validity condition here
   -- ex: AND IS NOT banned
-- And returns all the infos of the user (Acts somthing like SELECT * FROM updated_api_users)
RETURNING *;