1

I have a quote_item table where each item has the following fields: name, product_id, r_code1, r_code2

Product table also has r_code1 and r_code2

That data is in two locations because the user wanted quotes to be a snapshot, if they change r_code1 later at the product level it shouldn't impact quotes automatically.

However I've now been asked to reflect r_code changes to existing quotes since there was a mistake.

If I have a quote with 20 quote_items, how would I write a query that updates each quote_item.rcode_1 to the corresponding product.rcode_1. The link is through quote_item.product_id = product.id.

Thanks in advance Nick

2
  • Which DBMS are you using? Postgres? Oracle? Commented Jul 19, 2015 at 7:12
  • postgres, sorry to leave that out. Commented Jul 21, 2015 at 0:36

2 Answers 2

2
update qi set qi.rcode_1 = product.rcode_1 
from quote_items qi
    inner join product 
        on product.id = qi.product_id
Sign up to request clarification or add additional context in comments.

Comments

1

The following is standard SQL that would work in any database:

update qi
    set rcode_1 = (select p.rcode_1 from product p where p.id = qi.product_id);

There are other ways to express this, depending on the database, but the above should work in any database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.