cicakman
cicakman

Reputation: 1990

Create new records based on primary key of different table mysql

Let say I have two tables called users and user_profiles

user_profiles is empty without any rows, meanwhile users table has about 10-20k records.

I want to create empty records for user_profiles based upon users primary key

for question sake,

lets assume that

my users table has

id name email

my user_profiles table has

id user_id data

Is it possible to do strictly via SQL without involving any server-side script?

The end outcome, whatever amount of users I have, there should be rows of user_profiles matching this.

Upvotes: 0

Views: 28

Answers (1)

Ghazanfar Mir
Ghazanfar Mir

Reputation: 3543

Try following provided that your tables are structured properly with ALLOW NULL and auto_increment for user_profiles properties.

INSERT INTO user_profiles (user_id)
SELECT id FROM users

Upvotes: 1

Related Questions