【问题标题】:Create new records based on primary key of different table mysql根据不同表mysql的主键创建新记录
【发布时间】:2014-07-31 12:53:49
【问题描述】:

假设我有两个表,分别名为 usersuser_profiles

user_profiles 是空的,没有任何行,同时users 表有大约 10-20k 条记录。

我想根据users主键为user_profiles创建空记录

为了提问,

让我们假设

我的users 表有

idnameemail

我的user_profiles 表有

iduser_iddata

是否可以在不涉及任何服务器端脚本的情况下严格通过 SQL 执行?

最终结果,无论我拥有多少用户,都应该有几行与之匹配的 user_profiles。

【问题讨论】:

  • 你试过了吗?

标签: mysql sql


【解决方案1】:

如果您的表结构正确,请尝试以下操作,并使用 ALLOW NULL 和 auto_increment for user_profiles 属性。

INSERT INTO user_profiles (user_id)
SELECT id FROM users

【讨论】:

  • 不客气 - 如果有效,请在回答中打勾。
  • 一个简单的问题,如果我想为两列做两列,它应该像INSERT INTO user_profiles (created, user_id) VALUES (CURRENT_DATE(), SELECT id FROM users) 一样吗?
  • 试一试,如果不起作用,请告诉我。先尝试自己没有坏处:)
  • 试过了,不行。 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT id FROM users)' at line 1
  • 试试INSERT INTO user_profiles (created, user_id) SELECT CURRENT_DATE(), id FROM users
猜你喜欢
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多