简体   繁体   中英

Mysql update with data from another table

I have two tables buffer_details and stock. I want to update the stock table with data from the buffer details table.

CREATE TABLE `buffer_details` (  `Ref` varchar(20) COLLATE utf8_unicode_ci NOT NULL,   `Reference_produit` varchar(20) COLLATE utf8_unicode_ci NOT NULL,  `Qte` decimal(10,2) NOT NULL,   PRIMARY KEY (`Reference_produit`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT;

CREATE TABLE `stock` (`Reference` varchar(20) COLLATE utf8_unicode_ci NOT NULL, `qte` decimal(10,2) NOT NULL DEFAULT '0.00',  PRIMARY KEY (`Reference`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I tried this but it does not work , I am using Mysql. Please note that it is Qte = Qte + (buffer_details.Qte)

Update (select * from buffer_details) AS D1,stock set stock.qte = stock.qte + D1.Qte WHERE stock.Reference = D1.Reference_produit

You can use the update-join syntax:

UPDATE stock s
JOIN   buffer_details d1 ON s.reference = d1.reference_produit
SET    s.qte = s.qte + d1.qte

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM