I have two tables TableA and TableB with colums id, login, pass. Also I have a php array: $array = [1,3,5]. How can I transfer rows from TableA to TableB where id equal each value of my array? id is autoincremented and must be unique.
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?
Advertisement
Answer
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.