简体   繁体   中英

Insert with if condition

I need to create a sql statement that does this work: let's say I have two tables A and B containing integer fields. What I have to achieve is:

if (!(C is contained into A)) insert C into B

I'm using SQLite. Thanks for help

Actually, in your specific case it may happen to be as easy as

 insert into B (c_value) select c_value from A where c_value = @your_c_value_here 

see INSERT statement


sorry I haven't noticed the negation in your question for the C in A condition I have another option for you

with temp_val as (select @your_val_goes_here as val)
insert into b 
select val from temp_val where not exists
(select 1 from a where c = val)

check out this fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM