简体   繁体   English

基于不同表的mysql主键创建新记录

[英]Create new records based on primary key of different table mysql

Let say I have two tables called users and user_profiles 假设我有两个表,分别称为usersuser_profiles

user_profiles is empty without any rows, meanwhile users table has about 10-20k records. user_profiles为空,没有任何行,而users表中有大约10-20k条记录。

I want to create empty records for user_profiles based upon users primary key 我想根据users主键为user_profiles创建空记录

for question sake, 出于疑问,

lets assume that 让我们假设

my users table has 我的users表有

id name email id name email

my user_profiles table has 我的user_profiles表有

id user_id data id user_id data

Is it possible to do strictly via SQL without involving any server-side script? 是否可以通过SQL严格执行而不涉及任何服务器端脚本?

The end outcome, whatever amount of users I have, there should be rows of user_profiles matching this. 最终结果,无论我有多少用户,都应该有与之匹配的user_profiles行。

Try following provided that your tables are structured properly with ALLOW NULL and auto_increment for user_profiles properties. 如果您的表使用user_profiles属性的ALLOW NULL和auto_increment进行了正确的构造,请尝试以下操作。

INSERT INTO user_profiles (user_id)
SELECT id FROM users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM