在表之间移动MySQL行?

时间:2013-03-14 16:51:50

标签: php mysql database

我正在尝试从table_a(ptb_registrations)插入一些列INTO table_b(ptb_users)。

目前我有这个查询从表a更新表b中的列,但是它不是覆盖当前存储在ptb_users中的现有信息,而是希望它插入一个新行。

我的表ptb_users看起来像这样:

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

...我的表ptb_registrations看起来像这样:

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

现在我想将ptb_registrations中的第一个名称和电子邮件插入ptb_users.first_name和ptb_users.email作为新行?

有人可以指出我正确的方向吗?

这适用于更新

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

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

我试过了:

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

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

2 个答案:

答案 0 :(得分:0)

这是一个记录MySQL http://dev.mysql.com/doc/refman/5.5/en/insert.html

中的INSERT的链接
  INSERT INTO ptb_users (first_name, email)
  SELECT firstname, email
  FROM ptb_registrations;

答案 1 :(得分:0)

你想要做这样的插入语句:

INSERT INTO ptb_users (firstname)
  SELECT firstname
  FROM ptb_registrations
相关问题