rcgeorge23
rcgeorge23

Reputation: 3694

Insert data into join table

I have 3 tables, USER, ROLE and USER_ROLE.

USER_ROLE is a join table between USER and ROLE and looks like this:

USER_ID | ROLE_ID
1       | 1
1       | 2
2       | 1
2       | 2
...

I want to write some SQL that inserts every role once for each user. How do I go about doing this?

Upvotes: 0

Views: 88

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

You can do this with a cross join:

insert into user_role(user_id, role_id)
    select u.user_id, r.role_id
    from user u cross join role r;

Upvotes: 1

Related Questions