How to Perform an UPSERT so that I can use both new and old values in update part
Stupid but simple example: Assume I have a table 'Item' where I keeps totals of the items that receive. Item_Name Items_In_Stock Item name is primary key here. How to i achieve the following when ever I receive item A in quantity X. If the item does not exist, I insert a new recored for Item A and set the items in stock to X and if there exists a record where items in stock was Y then the new value in items in stock is (X + Y) INSERT INTO `item` (`item_name`, items_in_stock) VALUES( 'A', 27) ON DUPLICATE KEY UPDATE `new_items_count` = 27 + (SELECT items_in_stock where item_name = 'A' ) My