0

I'm trying to update products' prices in table A with prices from table B based on their product code.

There are about 50,000 products in table A but only 200 in table B. So I want those 200 prices in table B to replace the prices of those products in table A by matching the product code in both tables.

Can anyone advise me on how to go about doing it?

  • 1
    Which is your database? Sql server, oracle, MySql etc. – TechDo Sep 26 '13 at 06:05
  • Have you tried something to do? – Serge P Sep 26 '13 at 06:08
  • 1
    My database is on sql server. I tried to get some references from sites and edited my sql statement to be this. UPDATE dbo.Shelflabel_Pricechange_Master SET [Unit Price] = (SELECT t2.[Unit Price] FROM dbo.shelflabel_pricechange t2 WHERE dbo.Shelflabel_Pricechange_Master.Plu = t2.PLU) WHERE EXISTS (SELECT 1 FROM dbo.shelflabel_pricechange t2 WHERE dbo.Shelflabel_Pricechange_Master.Plu = t2.PLU) – user2818108 Sep 26 '13 at 06:08

3 Answers3

1
UPDATE  TableA
SET     TableA.ProductPrice= TableB.price
FROM    TableA
INNER JOIN  TableB
ON  TableA.productCode = TableB.productCode
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
0

You can use inner join update for MS Sql server(reference: Update a table using JOIN in SQL Server?). Please try below query:

UPDATE 
 TableA
SET TableA.Price = TableB.Price
FROM 
 TableA, TableB
WHERE 
 TableA.ProductCode = TableB.ProductCode
Community
  • 1
  • 1
TechDo
  • 18,398
  • 3
  • 51
  • 64
  • Isn't it preferable to use explicit join syntax rather than the old FROM Table,Table. Only reason i upvoted @Nadeem_MK answer. – ZeRaTuL_jF Apr 15 '15 at 16:17
0

try this command in mysql

update A t
set cost=(select cost from B where id=t.id)
where t.id in (select id from B)
Dev
  • 3,410
  • 4
  • 17
  • 16