Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Last active December 22, 2015 15:29
Show Gist options
  • Save rangercyh/6493075 to your computer and use it in GitHub Desktop.
Save rangercyh/6493075 to your computer and use it in GitHub Desktop.
SQL更新列数据,如果UNIQUE约束的key有重复的,则执行更新,否则插入一条新的
INSERT INTO test_tong VALUES
("aaa", 9), ("asdf", 9), ("bbb", 9)
ON DUPLICATE KEY UPDATE
score = VALUES(score)
UPDATE test_tong SET
score = CASE tongname
WHEN "aaa" THEN 1
WHEN "asdf" THEN 2
WHEN "bbb" THEN 3
END
WHERE tongname IN("aaa", "asdf", "bbb")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment