12

我需要在 Oracle 中执行以下 MySQL 查询:

INSERT INTO users VALUES(1,10) ON DUPLICATE KEY UPDATE points = 10;

除此之外还有别的merge吗?我只是不明白。

4

3 回答 3

14

您将需要使用MERGE. 就像是

MERGE INTO users dest
  USING( SELECT 1 user_id, 10 points FROM dual) src
     ON( dest.user_id = src.user_id )
 WHEN MATCHED THEN
   UPDATE SET points = src.points
 WHEN NOT MATCHED THEN
   INSERT( user_id, points ) 
     VALUES( src.user_id, src.points );
于 2012-05-14T19:04:44.437 回答
6

如果您不想使用 MERGE,可以尝试:

begin
   INSERT INTO users VALUES(1,10);

   exception
      when dup_val_on_index then
         update users
            set points = 10
          where id = 1;
end;
于 2012-05-15T07:43:42.257 回答
4
MERGE INTO users u
USING (SELECT 1 as id FROM dual) a
ON a.id = u.id
WHEN MATCHED THEN UPDATE SET u.points = 10
WHEN NOT MATCHED THEN INSERT (id, points) VALUES (1, 10);
于 2012-05-14T19:07:30.503 回答