简体   繁体   English

如何将多行从一个表转移到另一个表 (SQL)

[英]How to transfer several rows from one table to other table (SQL)

I have two tables TableA and TableB with colums id , login , pass .我有两个表TableATableB与列idloginpass Also I have a php array: $array = [1,3,5] .我还有一个 php 数组: $array = [1,3,5] How can I transfer rows from TableA to TableB where id equal each value of my array?如何将行从TableA传输到TableB ,其中id等于数组的每个值? id is autoincremented and must be unique. id是自动递增的,并且必须是唯一的。 In my head it looks like this :在我的脑海中它看起来像这样:

INSERT INTO TableB (`login`, `pass`) 
SELECT `login`, `pass` FROM TableA WHERE `id` = $array[0] 
AND `id` = $array[1] AND `id` = $array[2];

But it does not work但它不起作用

Is there any chances to do it in cycle using WHILE ?有没有机会使用WHILE循环进行?

You can use a statement such as this:您可以使用如下语句:

$ids = [1,3,5];
$query = "INSERT INTO `TableB` (`login`, `pass`) SELECT `login`,`pass` FROM `TableA` WHERE `id` IN (".implode(",", $ids).");";

Although: You should be using prepared statements and parameters for this, this answer is just meant to show the syntax.虽然:您应该为此使用准备好的语句和参数,但此答案仅用于显示语法。

it is easy let's say you are using mysqli:假设您正在使用 mysqli,这很容易:

$query= "SELECT * FROM TableA limit 3";
//takes first 3 rows from TableA
//$con being the connection string in config file
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
//takes each row by row
$username = $row['username'];
$password = $row['password'];
//now that we initiated variables the sql query
$query1 = "INSERT INTO TableB (username, password) VALUES ('$username', '$password')";
mysqli_query($con,$query1);
}

Hope this helps希望这可以帮助

EDIT: I just read that it should have an array so you should change your query to this:编辑:我刚刚读到它应该有一个数组,因此您应该将查询更改为:

for($i=0; $i<count(array), $i++){

$query= "SELECT * FROM TableA WHERE ID = '$array[i]'";

//$con being the connection string in config file
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result){
//takes each row by row
$username = $row['username'];
$password = $row['password'];
//now that we initiated variables the sql query
$query1 = "INSERT INTO TableB (username, password) VALUES ('$username', '$password')";
mysqli_query($con,$query1);
}
}

暂无
暂无

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

相关问题 SELECT sql语句,该语句从表中检索一行,并从另一表中检索许多相关行 - SELECT sql statement that retrieves one row from a table and many related rows from other table 如何将一行从一个表转移到另一个表? - How to transfer a row from one table to another? MySQL从一个表到另一个PHP传输8000+行 - mySQL transfer 8000+ rows from one table to another PHP 在 MySQL 中,如何从一个表中获取 2 列和从其他表中的一行中获取 2 行作为列? - How to get 2 columns from one table and 2 rows as columns from other table in one row, in MySQL? 如何从一个表中选择具有同一表中其他行的特定ID的行 - how to select rows from one table with specific id of other rows in the same table 通过一个来自另一个表的字段从一个表中选择行 - Selecting rows from a table by One a field from other table sql从表中选择,然后在一个语句中与其他表进行比较 - sql select from table then compare with other table in one statement 将多行从一张表转移到另一张表 - Transfer multiple rows from 1 table to another 如何使用PHP将SQL Server表中新添加的行转移到相同的MySQL表中? - How do I transfer newly added rows in an SQL Server table to an identical MySQL table using PHP? 从一个表动态获取行并从其他表获取值将其汇总并插入到目标表中 - fetch rows dynamically from one table and get values from other table sum it up and insert into target table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM