[英]How to insert multiple inputs into the same row that group by the same data
I have a system that records all the user's purchases.我有一个记录所有用户购买的系统。 In my system, I have a table called purchase_item .在我的系统中,我有一个名为purchase_item的表。
The table purchase_item looks like this表purchase_item看起来像这样
id ID | order_id order_id | product_name产品名称 |
---|---|---|
1 1个 | 10001 10001 | iphoneX苹果X |
2 2个 | 10001 10001 | ipnone 12 Pro ipnone 12 专业版 |
3 3个 | 10001 10001 | ipad ipad |
4 4个 | 10002 10002 | Apple pen苹果笔 |
5 5个 | 10002 10002 | Airpods气垫 |
6 6个 | 10002 10002 | Iphone 12 Pro Max iPhone 12 专业版 |
This is my query to insert the data above这是我插入上面数据的查询
$osql = "INSERT INTO `purchase_items`(`order_id`, `product_name`) VALUES ('$order_id',`$product_name` )";
Now i created another table( receipt_table ) that i want to insert the above data in group.现在我创建了另一个表( receipt_table ),我想在组中插入上述数据。 Anyone knows how can i do it?任何人都知道我该怎么做?
what I want is the output of my table to look like this:我想要的是我的表的 output 看起来像这样:
id ID | order_id order_id |
---|---|
1 1个 | 10001 10001 |
2 2个 | 10002 10002 |
I want to group my data by using "INSERT INTO".我想使用“INSERT INTO”对我的数据进行分组。 Anyone knows how can i do that?任何人都知道我该怎么做?
The way to retrieve the last inserted id in MySQL is $mysqli->insert_id
.在 MySQL 中检索最后插入的 id 的方法是$mysqli->insert_id
。
$mysqli = new mysqli($servername, $username, $password, $dbname);
$osql = "INSERT INTO `purchase_items`(`order_id`, `product_name`) VALUES (?, ?)";
$stmt = $mysqli->prepare($osql);
$stmt->bind_param("is", $order_id, $product_name);
$stmt->execute();
$id = $mysqli->insert_id;
$rsql = "INSERT INTO receipt_table (order_id) VALUES (?)";
$stmt = $conn->prepare($rsql);
$stmt->bind_param("i", $id);
$stmt->execute();
https://www.php.net/manual/en/mysqli.insert-id.phphttps://www.w3schools.com/php/php_mysql_prepared_statements.asp https://www.php.net/manual/en/mysqli.insert-id.phphttps://www.w3schools.com/php/php_mysql_prepared_statements.asp
Without knowing what sql dialect you use, but in sql server we could do like this:不知道你使用的是什么 sql 方言,但在sql 服务器中我们可以这样做:
INSERT INTO receipt_table(id, order_id)
select id, order_id
from purchase_items
group by id, order_id
<?php $purchased_item = [ [10001, "iPhoneX"], [10001, "iPhone 12 Pro"], [10001, "iPad"], [10002, "Apple Pen"], [10002, "Airpods"], [10002, "iPhone 12 Pro Max"] ]; $servername = "localhost"; $username = "root"; //your user name $password = ""; // your password here $dbname = "myDB"; // database name $conn = new mysqli($servername, $username, $password, $dbname); $osql = "INSERT INTO `purchase_items`(`order_id`, `product_name`) VALUES (?,? )"; $ostmt = $conn->prepare($osql); $isql = "INSERT INTO reciept_table(`id`, `order_id`) VALUES (?, ?)"; $istmt = $conn->prepare($isql); $lastId = null; // to check id is entered or not foreach ($purchased_item as $value) { $ostmt->bind_param("is", $value[0], $value[1]); $ostmt->execute(); if ($lastId != $value[0]){ $istmt->bind_param("i", $value[0]); $istmt->execute(); } $lastId = $vlaue[0]; } ?>
you can simple use a sql to insert grouped data.and I guess that receipt_table id is an auto_incremnt id.您可以简单地使用 sql 插入分组数据。我猜 receipt_table id 是一个 auto_incremnt id。
insert into
receipt_table(order_id)
select order_id from purchase_items
group by order_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.