我有带日志的表,当他们请求时->它们将可见状态更改为true。问题是我不需要更新已经看到的行,只需返回它们。有办法吗?
logs
id | seen
1 | false
2 | true
UPDATE logs as new
SET seen=true
FROM (SELECT * FROM logs WHERE seen is not null LIMIT 5) as old
WHERE new.id = old.id //if i add here "AND old.seen=false" then nothing will return
RETURNING old.seen
答案 0 :(得分:0)
您的子查询应该过滤掉“看不见的”记录,并且也不需要返回所有列:
(SELECT id FROM logs WHERE NOT seen LIMIT 5)
这将返回log
表中最多5条记录,其中seen
等于false
的ID。
更新后的解决方案如下:
UPDATE logs
SET seen = true
FROM (SELECT id FROM logs WHERE NOT seen LIMIT 5) AS unseen
WHERE logs.id = unseen.id
RETURNING id; -- Returning `seen` would only return false. `id` is more useful, no?