首先,
UNIQUE在名称,条形码,商品,位置和价格上添加一个约束。
ALTER TABLE tableX ADD ConSTRAINT tableX_UQ UNIQUE (name, barpre, item, location, price) ;
然后,您可以使用
INSERT INTO ... ON DUPLICATE KEY UPDATE:
INSERT INTO tableX (name, barpre, item, location, price, quantity, date)VALUES (?, ?, ?, ?, ?, ?, ?)ON DUPLICATE KEY UPDATE quantity = CASE WHEN VALUES(date) > date THEN quantity + VALUES(quantity) -- add quantity ELSE quantity -- or leave as it is END, date = CASE WHEN VALUES(date) > date THEN VALUES(date) ; -- set date to new date ELSE date -- or leave as it is END
REPLACE也可以使用,但是行为有所不同(如果您具有外键,则尤为重要)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)