You are on page 1of 3

using the following sample:

CREATE TABLE tbl(

x integer,

y integer

INSERT INTO tbl

SELECT g AS x FROM generate_series(1, 10) AS g;

Which gives:

x |y

----+---

1|

2|

3|

4|

5|

6|

7|

8|

9|

10 |

(10 rows)

I'd like to have something like (note - I do not care about the ordering of the three values updated in y
here):

x |y

----+---
1| 1

2| 2

3| 3

4|

5|

6|

7|

8|

9|

10 |

(10 rows)

I've tried to do this using the following:

UPDATE tbl

SET y = x

WHERE y IS NULL

LIMIT 3;

But I get the error ERROR: syntax error at or near "LIMIT", which I'm not sure how to fix.

Solution

I think the following works:

update tbl

set y = x

where x in (select x from tbl where y is null limit 3);

I can't think of a scenario this would fail, if there's a better approach it'd be good to know.
postgresqlupdatelimits

You might also like