简体   繁体   English

如何在具有相同ID的数据库表中显示组合的行

[英]How to display combined rows in the database table with same ID

i have a question for the experts. 我有一个问题要问专家。 I have no idea how to combine all the order ID with the same ID, 我不知道如何将所有订单ID与相同的ID组合在一起,

like in the picture, all orders with order ID = 1 are in one transaction, how do i combine all order IDs with the same id and display only one total price for all of the proudcts bought. 就像图片中一样,所有订单ID = 1的订单都在一次交易中,我如何将所有具有相同ID的订单ID合并在一起,并只显示购买的所有骄傲商品的一个总价。 and since all order ID are the same so the deliver and payment option will also be the same just need to display one row. 并且由于所有订单ID都相同,因此交付和付款选项也将相同,只需要显示一行即可。

and a follow up question is how will i also show all the products bought if i combine all the same order IDs, compute the total price and display delivery and payment option in one single row 接下来的问题是,如果我将所有相同的订单ID组合在一起,计算总价格并在一行中显示交货和付款方式,我还将如何显示所有购买的产品

在此处输入图片说明

Here is the picture for my database relationship, i just followed the ones on the internet. 这是我的数据库关系的图片,我只是在互联网上关注这些图片。 the only difference is that "products" are changed to "inventory" and "serial" in products are changed to "prod_id" 唯一的区别是“产品”更改为“库存”,产品中的“序列”更改为“ prod_id”

在此处输入图片说明

here are my codes to display the current picture. 这是我显示当前图片的代码。

<?php
    session_start();
    $conn = @mysql_connect("localhost","root","");
    $db = @mysql_select_db("");

$qry = "SELECT customers.name,customers.payment,customers.carrier, orders.date, order_detail.productid, order_detail.quantity, order_detail.price, order_detail.orderid, inventory.prod_name 
        FROM customers 
        RIGHT JOIN orders on customers.serial=orders.serial 
        RIGHT JOIN order_detail on orders.serial=order_detail.orderid 
        LEFT JOIN inventory on order_detail.productid=inventory.prod_id where customers.email='{$_SESSION['email']}'";

mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
echo "<center>";
echo "<table class='CSSTableGenerator'>
<tr>

<td>Date of Purchase</td>
<td>First Name</td>
<td>Order ID</td>
<td>Products</td>
<td>Quantity</td>
<td>Total Price</td>
<td>Delivery Option</td>
<td>Payment Option</td>

<tr>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['orderid']."</td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".($row['price']*$row['quantity'])."</td>";
echo "<td>".$row['carrier']."</td>";
echo "<td>".$row['payment']."</td>";
echo "</tr>";
    }
echo "</table>";
?>
</table>

It seems that what you're needing to learn for this particular question is the SQL Group By clause. 似乎您需要为该特定问题学习的是SQL Group By子句。

This (untested, might need minor tweaking) should do the trick. 这(未经测试,可能需要进行一些微调)应该可以解决问题。 Note that you don't get order item quantities out of this, but I would say that it's possible to get them. 请注意,您不会从中得到订单物料的数量,但是我想说有可能得到它们。

SELECT o.date, c.name, o.serial as 'Order Id', GROUP_CONCAT(p.name) as 'Products', SUM(od.price) as 'Total', c.carrier, c.payment
FROM orders o
JOIN customers c on o.customer_id = c.serial
JOIN orderdetail od on o.serial = od.orderid
JOIN products p on od.productid = products.prod_id
GROUP BY o.serial

Of particular note is a handy aggregate function in MySQL called GROUP_CONCAT. 需要特别注意的是MySQL中一个方便的聚合函数,称为GROUP_CONCAT。 http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat http://dev.mysql.com/doc/refman/5.0/zh/group-by-functions.html#function_group-concat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM