I simply want to replace (update) DATE, PRICE and OLDPRICE when a price is different than the PRICE in my table where MODEL is unique.
Sample Row Table Data:
DATE | MAKE | MODEL | PRICE | OLDPRICE
2012-04-15 | Advance | DTA-100-X | 400 | 390
There must be a dozen ways to do this but I'm looking for the best solution to use in a MySQL query.
Should I be using:
INSERT INTO..ON DUPLICATE KEY UPDATEREPLACE INTO..UPDATE...WHERE PRICE != XXXEssential syntax would be to UPDATE if the MODEL is the same but the PRICE is different; OLDPRICE becomes PRICE on UPDATE
*UPDATE*
This REPLACES whether price changed or not. I only want updates/replaces if price changed ie, this should NOT update anything given above example, however it does because date is different:
REPLACE INTO MyTable (DATE, MAKE, MODEL, PRICE, OLDPRICE) VALUES ('2012-10-02', 'Advance', 'DTA-100-X', '400', '390')
MySQL has a REPLACE statement specifically for this purpose.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
-- http://dev.mysql.com/doc/refman/5.6/en/replace.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With