[英]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.