Another option:
INSERT INTO tbl (count, otherID)
VALUES (2, 'a')
ON DUPLICATE KEY UPDATE
count = GREATEST(VALUES(count), count) ;
Warning: This will fail if the passed value for count
is NULL
(instead of 2
). It will update the column with NULL
. So, it's better to use the IF()
or a CASE
clause.
Unless you prefer the (there goes the elegance ...):
ON DUPLICATE KEY UPDATE
count = GREATEST(COALESCE(VALUES(count), count), count) ;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…