I have three tables, a User table, a Company table, and a linking table that assigns user roles. I am trying to update the user_role table so that all users of their associated companies have the same role. The role table looks something like this:
+---------+---------+------------+
| role_id | user_id | company_id |
+---------+---------+------------+
| 2 | 54 | 30 |
| 15 | 1 | 15 |
| 14 | 87 | 32 |
| 15 | 88 | 33 |
| 15 | 106 | 3 |
+---------+---------+------------+
What I want to logically do is something like this
INSERT INTO user_role
(user_id, company_id, role_id)
VALUES (
(SELECT user_id FROM user_role WHERE company_id = ##FIRST COMPANY_ID IN LIST##),
(##LIST OF COMPANY_ID'S##), 2
);
I have a static list of company_id’s, and I want all users that have any role in that company to be assigned an additional static role_id in this table. What is the best way to accomplish this? Should I look at a stored procedure?
1 Answer