简体   繁体   English

选择并插入mysql

[英]select and insert mysql

I would like to select one column from other table and insert it into another one. 我想从其他表格中选择一列,然后将其插入另一列中。

I have column named image in table2 and want to select that and insert along with the below data to table1. 我在table2中有一个名为image列,想要选择该列并将其与以下数据一起插入到table1中。 is that possible? 那可能吗?

$stmt = $mysqli->prepare("insert into table1 (username, firstname, lastname, image) ")
$stmt->bind_param('sss', $username, $fname, $lname);
$stmt->execute();

Yes, it's possible: 是的,有可能:

$stmt = $mysqli->prepare("
  insert into table1 (username, firstname, lastname, image) 
  select ?,?,?,image from table2
  ");

...but I hope table2 has only one row! ...但是我希望table2只有一行!

You could do a INSERT ... SELECT query - http://dev.mysql.com/doc/refman/5.1/en/insert-select.html 您可以执行INSERT ... SELECT查询-http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

INSERT INTO table1 (username, firstname, lastname, image)
SELECT ?, ?, ?, image FROM table2 t2 WHERE t2.image_id = ?

The first 3 ? 前3个? are your bound params - $username, $fname, $lname 是您绑定的参数- $username, $fname, $lname
t2.image_id = ? represents the id/unique field of your desired img. 代表所需img的ID /唯一字段。

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

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