TheBAST
TheBAST

Reputation: 2736

How to insert entries in a related model with a corresponding condition met from the parent table

SO I have a tablr users table whose structure is

id | name | email | password | role_id

and it has 100 entries in it.

I have a couriers table whose structure is

id | user_id | so on.

I would like to populate the couriers table with the value in the users table that has the role_id of 3.

So I have 40 couriers and I want them to be inserted into the couriers table.

Upvotes: 0

Views: 30

Answers (1)

Rohan Pillai
Rohan Pillai

Reputation: 967

You can use the syntax:

INSERT INTO couriers (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM users
WHERE condition;

Upvotes: 1

Related Questions