需求:
根据某个主键查询,如果存在某条记录,就更新这条记录,否则新增一条记录
假设student表,结构如下
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | age | int | YES | | NULL | | | name | varchar(255) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+
假设记录是id=1, name='Terry', age=20
MySQL的写法
--MySQL,ID是主键,如果存在1就更新,否则新建 INSERT INTO student (id, name, age) VALUES(1, "Terry", 20) ON DUPLICATE KEY UPDATE name="Terry", age=20
Oracle写法
insert into STUDENT (ID, NAME, AGE) select 1, 'Terry', 20 from dual where not exists (select * from STUDENT where ID=1)
文章评论