【发布时间】:2019-07-13 06:08:04
【问题描述】:
我有桌 foo 和 PK int id,varchar state
table foo:
- int id
- varchar state
- int code1
- int code2
如果记录不存在,我想做一个 sql 插入。
要完成插入语句,我必须使用插入选择检索一些信息。
在输入中我有:
id = 1, state = 'A'
在插入中,我必须用插入选择检索到的值填充另一个字段。
code1 and code2 where id = 1 (they are always the same for id = x)
通常我必须这样做:
1- 使用where id=1 and state=1 进行选择,如果不存在,我将不得不执行 sql 插入
2-如果记录不存在,则检索字段code1 and code2 when id=1的值
3- 插入
是否可以在一个查询中完成所有操作?
select * from foo where id=1 and state = 'A'
如果不存在:
select code1,code2 from foo where id=1; #1,2
insert into foo(id, state, code1, code2) values (1,'A', 1, 2);
是否可以合并到一个查询中?
谢谢
【问题讨论】:
-
根据你的移动目标问题调整了答案;)
标签: sql oracle select insert insert-select