Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

This question already has an answer here:

SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column? 9 answers

I have two tables...

table1 ( id, item, price ) values:

id | item | price

-------------

10 | book | 20  

20 | copy | 30   

30 | pen  | 10

....table2 ( id, item, price) values:

id | item | price

-------------

10 | book | 20

20 | book | 30

Now I want to:

update table1 

   set table1.Price = table2.price 

 where table1.id = table2.id

   and table1.item = table2.item.

How do I do it?

1 Answer

0 votes
by (40.7k points)

You can try the below code:

UPDATE table1 

 SET table1.Price = table2.price 

 FROM table1  INNER JOIN  table2 ON table1.id = table2.id

Otherwise, you can try this:

UPDATE table1 

SET price=(SELECT price FROM table2 WHERE table1.id=table2.id);

Related questions

0 votes
1 answer
asked Jan 6, 2021 in SQL by Appu (6.1k points)
0 votes
2 answers
0 votes
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...