Emi AG
Emi AG

Reputation: 3

SQL - How to insert into table based on id's found in another table?

I would need something like this pseudo code:

Upvotes: 0

Views: 63

Answers (1)

EdmCoff
EdmCoff

Reputation: 3576

I think you are probably looking for something like

INSERT INTO userauthorizations
 SELECT DISTINCT userid, 11
 FROM userauthorizations ua1
 WHERE NOT EXISTS 
  (SELECT *
  FROM userauthorizations ua2 
  WHERE ua2.userid = ua1.userid 
   AND ua2.authid = 11)

This will find all users who do not currently have a authid=11 and add a row for that userid with authid=11.

Note that this is different from your pseudocode (SELECT DISTINCT USERID FROM USERAUTHORIZATIONS WHERE AUTHID <> 11), which finds users who have at least one entry that isn't authid=11.

Upvotes: 1

Related Questions