当您升级一行(PostgreSQL> = 9.5),并且希望可能的INSERT与可能的UPDATE完全相同时,可以这样编写:
INSERT INTO tablename (id, username, password, level, email)
VALUES (1, 'John', 'qwerty', 5, 'john@mail.com')
ON CONFLICT (id) DO UPDATE SET
id=EXCLUDED.id, username=EXCLUDED.username,
password=EXCLUDED.password, level=EXCLUDED.level,email=EXCLUDED.email
有没有更短的方法?只能说:使用所有EXCLUDE值。
在SQLite中,我曾经做过:
INSERT OR REPLACE INTO tablename (id, user, password, level, email)
VALUES (1, 'John', 'qwerty', 5, 'john@mail.com')
另一种选择是使用jsonb列,这样你就不必对列的担心
—
Ĵ将
INSERT INTO tablename (id, username, password, level, email) VALUES (1, 'John', 'qwerty', 5, 'john@mail.com') ON CONFLICT (id) DO UPDATE SET (username, password, level, email) = (EXCLUDED.username, EXCLUDED.password, EXCLUDED.level, EXCLUDED.email).
几乎相同,但易于复制/粘贴/管理列列表