简体   繁体   English

在表之间移动MySQL行?

[英]Move MySQL rows between tables?

I am trying to insert some columns from table_a (ptb_registrations) INTO table_b (ptb_users). 我正在尝试从table_a(ptb_registrations)INTO table_b(ptb_users)插入一些列。

At the moment I have this query which updates the columns in table b from table a, but instead of it overwriting existing information which is currently stored in ptb_users I want it to insert a new row. 目前,我有此查询,它从表a更新表b中的列,但不是覆盖现有存储在ptb_users中的现有信息,而是希望它插入新行。

My table ptb_users looks like this: 我的表ptb_users看起来像这样:

id (auto inc)    |   first_name   |         email   
     1                  john            john@email.com   

...and my table ptb_registrations looks like this: ...而我的表ptb_registrations看起来像这样:

id (auto inc)    |   firstname   |         email   
     2                  eric            john@email.com   

So now I want to insert the columns firstname and email from ptb_registrations into ptb_users.first_name and ptb_users.email as a new row? 所以现在我想将ptb_registrations中的firstname和email列插入ptb_users.first_name和ptb_users.email中作为新行?

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

This works on update 这适用于更新

$query = "UPDATE ptb_users
SET first_name = (
  SELECT firstname
  FROM ptb_registrations
)";

mysql_query($query)or die('Could not update members: ' . mysql_error());

I have tried: 我努力了:

$query = "INSERT INTO ptb_users.first_name = (
  SELECT firstname
  FROM ptb_registrations
)";

mysql_query($query)or die('Could not update members: ' . mysql_error());

Here's a link documenting INSERT in MySQL http://dev.mysql.com/doc/refman/5.5/en/insert.html 这是一个记录MySQL中的INSERT的链接http://dev.mysql.com/doc/refman/5.5/en/insert.html

  INSERT INTO ptb_users (first_name, email)
  SELECT firstname, email
  FROM ptb_registrations;

You'll want to do your insert statement like this: 您将需要执行以下插入语句:

INSERT INTO ptb_users (firstname)
  SELECT firstname
  FROM ptb_registrations

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

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