Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limiting postgreSQL update command

Tags:

I have a table with given structure, now I want to write a query that will transfer 2 xx product from status 1 to say status 2. the child code is irrelevant presently to me.

master_code| child_code | status_code -----------|------------|------------     xx     |    xx1     |     1     xx     |    xx2     |     1     xx     |    xx3     |     1     xx     |    xx4     |     2     xx     |    xx5     |     2     yy     |    yy1     |     3     yy     |    yy2     |     2     zz     |    zz1     |     1     zz     |    zz2     |     1 

I have implemented basic checks, and when I used

update only product_child set product_status=1 where product_status=2 

all three xx's got code 2, I want control over that, I was expecting only one xx will get code change with this command

like image 279
Mohit Avatar asked Jul 11 '12 11:07

Mohit


People also ask

How do I limit data in PostgreSQL?

The LIMIT clause can be used with the OFFSET clause to skip a specific number of rows before returning the query for the LIMIT clause. Syntax:SELECT * FROM table LIMIT n OFFSET m; Let's analyze the syntax above. The LIMIT clause returns a subset of “n” rows from the query result.

Does PostgreSQL support limit?

PostgreSQL does not impose a limit on the number of rows in any table. There is no PostgreSQL-imposed limit on the number of indexes you can create on a table. Of course, performance may degrade if you choose to create more and more indexes on a table with more and more columns.

How many updates per second can Postgres handle?

When using Postgres if you do need writes exceeding 10,000s of INSERT s per second we turn to the Postgres COPY utility for bulk loading. COPY is capable of handling 100,000s of writes per second.

How does limit and offset work in PostgreSQL?

If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the query itself yields fewer rows). LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT with a NULL argument. OFFSET says to skip that many rows before beginning to return rows.


2 Answers

If you do not care which row gets updated, something I would be very wary of doing (please add a PK to the table for this really), then you could use something like the following:

UPDATE     product_child SET     product_status = 1 WHERE     CTID IN ( SELECT CTID FROM product_child WHERE product_status = 2 and master_code = 'xx' LIMIT 1 ) 

CTID is a unique row identifier - and by limiting the subselect to 1 record we get back one CTID corresponding to a row that meets the WHERE clause.

like image 187
John D Avatar answered Sep 19 '22 06:09

John D


i found a way

update only product_child set product_status =1 where product_child_code in (select product_child_code                 from product_child                 where product_code = get_product_code('Baby Crib')                  and product_status = 2                  limit 5) 
like image 28
Mohit Avatar answered Sep 19 '22 06:09

Mohit