MYSQL INSERT or UPDATE IF

前端 未结 3 978
清酒与你
清酒与你 2020-12-16 06:10

Been looking around the web for a while now and don\'t seem to be able to come across anything similar to what I want. I know it\'s something to do with the way I\'m writing

3条回答
  •  再見小時候
    2020-12-16 06:47

    Thanks to all the help from those above the solution I found in the end was very close to both. Find it below:

    INSERT INTO `stock`
        (name, barcode, item, quantity, location, price, date)  
    VALUES
        (?,?,?,?,?,?,?)                         
    ON DUPLICATE KEY UPDATE
         quantity = CASE WHEN 
                    VALUES(date) < $date
                    THEN quantity + $quantity
                    ELSE quantity 
                    END,
        date = CASE WHEN 
                    VALUES(date) < $date
                    THEN VALUES(date)
                    ELSE $date
                    END
    

提交回复
热议问题