除了我想从$_POST["user"]
插入值的一个字段外,有没有办法插入select语句中的大部分值?我需要两个查询才能这样做吗?
$sql = "INSERT INTO `TAB1` (name,category, brand, image_url,big_img)
SELECT a.name,b.category, b.brand, a.image_url,a.big_img
FROM `mTaB2` a INNER JOIN `mTab3` b
ON a.tid = b.id
WHERE a.id = ?
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_POST["id"]);
$result = $users->execute();
...............
$sql = "INSERT INTO `TAB1` (user)
VALUES(?)
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_POST["user"]);
$result = $users->execute();
答案 0 :(得分:3)
我认为这样的事情会奏效:
$sql = "INSERT INTO `TAB1`(user, name, category, brand, image_url, big_img)
SELECT ?, a.name, b.category, b.brand, a.image_url, a.big_img
FROM `mTaB2` a INNER JOIN `mTab3` b
ON a.tid = b.id
WHERE a.id = ?
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_POST["user"]);
$users->bindValue(2, $_POST["id"]);
答案 1 :(得分:2)
不需要两个查询:
$sql = "INSERT INTO `TAB1` (user,name,category, brand, image_url,big_img)
SELECT ?,a.name,b.category, b.brand, a.image_url,a.big_img
FROM `mTaB2` a INNER JOIN `mTab3` b
ON a.tid = b.id
WHERE a.id = ?
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_POST["user"]);
$users->bindValue(2, $_POST["id"]);
$result = $users->execute();