[英]Oracle SQL: INSERT with SELECT
我有一个这样的查询:
insert into book ('book_id', 'category_id', 'book_name', 'buy_price', 'sell_price') values ('B-001, 'BOM-001', 'Tarzan', 200, 300);
是否可以使用select和条件where category_name = 'adventure'所以我得到BOM-001从另一个表中获取category_id ? 谁能给我一个该查询的样本?
谢谢你
这看起来像:
insert into book (book_id, category_id, book_name, buy_price, sell_price)
select ?, c.category_id, ?, ?, ?
from category c
where c.category_name = ?;
?
是常量值的占位符。 请注意, insert
的列名周围没有单引号。
有两种方法可以实现您想要的。
第一:单个值总是可以被返回单个值的查询替换:
insert into book (book_id, category_id, book_name, buy_price, sell_price)
values ('B-001',
(select category_id from category where category_name = 'adventure'),
'Tarzan',
200,
300
);
第二:您可以从某处插入您 select 的行:
insert into book (book_id, category_id, book_name, buy_price, sell_price)
select 'B-001', category_id, 'Tarzan', 200, 300
from category where category_name = 'adventure';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.