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 problem is that i have multiple column in my actual table. Is it a good idea to write multiple select statements in the update part?
Of course I can do it in code but is there a better way?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…