通过来自另一个 table 的 SELECT 数据插入以及额外的手动数据 PHP SQL

INSERT through SELECT data from another table with additional manual data PHP SQL

我正在尝试从另一个 table 插入数据,它不起作用我阅读了 Whosebug 和 w3school 上的所有线程,但基于它我的 SQL 不起作用。 我正在尝试将 table pincode WHERE state = US 中可用的所有数据一一插入 我认为我的代码是正确的,但它没有插入数据。我从 pincode table 和其他人中选择了大约三个列,因为我在 PHP

中手动分配执行数组中的参数

请帮忙,我如何将数据从 pincode 插入到 table_alloted

请帮忙..

table = 密码

编号 pin_code 状态
1 123456 distname1 美国
2 123457 distname2 美国
3 123458 distname3 美国
4 123459 distname4 美国

table = table_alloted

编号 pin_code 状态 tech_id insert_dt
1 123456 distname1 美国 ts12563 17-06-2021
2 123457 distname2 美国 ts12563 17-06-2021
3 123458 distname3 美国 ts12563 17-06-2021
4 123459 distname4 美国 ts12563 17-06-2021

代码:

$sqlInsert= $con->prepare("INSERT INTO `table_alloted` (`tech_id `, `insert_dt`, `pin_code`, `district`, `state`) VALUES(:tech_id, :my_date, (SELECT `pin_code`, `district`, `state` FROM `pincode` WHERE `state` = :state)");
$sqlInsert->execute(array(
  ':tech_id' => 'ts12563',
  ':my_date' => date('d-m-Y'),
  ':state' => 'US'
));

使用下面的查询

INSERT INTO `table_alloted` (`tech_id `, `insert_dt`, `pin_code`, `district`, `state`)
SELECT :tech_id, :my_date, `pin_code`, `district`, `state` FROM `pincode` WHERE `state` = :state

我不确定您在 PHP 中的语法,但在数据库中上述查询将起作用。

根据@Amit Verma 查询的 PHP 代码下方:

//Prepare statement
$sqlInsert = $con->prepare(
    "INSERT INTO `table_alloted` (`tech_id`, `insert_dt`, `pin_code`, `district`, `state`) 
    SELECT :tech_id, :my_date, `pin_code`, `district`, `state` FROM `pincode` WHERE `state` = :state"
);
//Execute insert
$sqlInsert->execute([
    ":tech_id" => "ts12563",
    ":my_date" => date("Y-m-d"),
    ":state" => "US",
]);
//Check result
$query = "SELECT * FROM `table_alloted`;";
$stmt = $pdo->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

print_r($rows);

PHP & MySQL online playground