我有這樣的查詢
insert into states(state_name,cust_id)
select 'Sleeping',customer.cust_id from customer;
我有一個狀態表和客戶表,對于每個客戶,我想在狀態表中插入一行,狀態為“睡眠”。
假設我在客戶表中有三行,上面的查詢將在狀態表中插入三行,其中包含來自客戶表的相應 cust_id。
現在,在此查詢中,我只想確保僅在表中不存在該狀態時才插入。我目前被困在這里,不確定如何在此查詢中放置該檢查。誰能幫我 ?
uj5u.com熱心網友回復:
使用merge來代替:
merge into states s
using customer c
on ( c.cust_id = s.cust_id
and s.state_name =' Sleeping')
when not matched then insert (state_name, cust_id)
values ('Sleeping', c.cust_id);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367879.html
