任何人都可以帮我解决mysql查询问题。我试图使用insert select语句插入多行。
if ($insert_stmt = $mysqli->prepare("INSERT INTO order_processing_info (order_id, cart_item_id, item_id, price, quantity) VALUES (?, (SELECT s.cart_item_id, i.item_id, i.price, s.quantity FROM inventory_info AS i, cart_info AS s WHERE i.item_id=s.item_id AND s.user_id = ?))")) {
$insert_stmt->bind_param('ss', $order_id, $user_id);
我认为问题是因为我想在这里插入所有插入相同的order_id,虽然我不确定。
之前我试图在select语句块中运行它失败。
if ($select_stmt = $mysqli->prepare("SELECT i.item_id, i.price, s.quantity, s.cart_item_id FROM inventory_info AS i, shopping_cart_info AS s WHERE i.item_id=s.item_id AND s.user_id = ?")) {
$select_stmt->bind_param("s", $user_id);
$select_stmt->execute();
$select_stmt->bind_result($item_id, $price, $quantity, $cart_item_id);
while ($select_stmt->fetch()) {
if ($insert_stmt = $mysqli->prepare("INSERT INTO order_processing_info (order_id, cart_item_id, item_id, price, quantity) VALUES (?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('sssss', $order_id, $cart_item_id, $item_id, $price, $quantity);
// Execute the prepared query.
if (!$insert_stmt->execute()) {
header('Location: ../error.php?err=INSERT failure: INSERT');
exit();
}
}
}
$select_stmt->close();
}
我已经在stackoverflow上查了很多,但我想我不是那么精通数据库查询,所以如果有人可以帮我这个,我将不胜感激。
答案 0 :(得分:0)
使用“INSERT INTO SELECT”查询(http://www.w3schools.com/sql/sql_insert_into_select.asp),只需要绑定一个参数,即用户ID。它还可以节省循环中查询的开销