How to update top N records for a table in PostgreSQL?
Hello, I have a [
articlevote] table to help store the data vote of the user, it's related to some articles. That table look as below :
CREATE TABLE public.articlevote ( id SERIAL, answerid INTEGER , questionid INTEGER, createdby UUID, createddate TIMESTAMP )
Now I want to update top 3000 records in this table, set
answerid column value from 12 to 14.
UPDATE command only filter and update base on
WHERE command. it does not allow
TOP command. Please support me if you have any solutions.
Mary Christ Dec 31 2020
You can use
WITHcommand to help return a data set and you can update only on this data set. See an example below:
WITH cte AS ( select * from articlevote where answerid = 12 limit 3000 ) update articlevote set answerid = 14 where id in ( select id from cte );
I hope it's helpful for you.