user34537
user34537

Reputation:

insert into table when value doesnt exist?

I am trying to insert all entries in table b into table A when

  1. the status is <32 and

  2. it isnt already in table a.

The 2nd part is what is giving me trouble. I wrote a subquery and my gut says i did it wrong and i also notice it taking a very long time to execute

how do i write this rest of the query?

table a { int id, fId }
table b { int id, status; string data; }

insert into a(fId) select id from b where status<32 and ???

Upvotes: 0

Views: 180

Answers (1)

thelost
thelost

Reputation: 6694

insert into a(fId) 
  select id
  from b
  where status<32 
    and id not in (select fId from a)

Upvotes: 2

Related Questions