我正在用php和mysql做一个电子商务网站。 我想回显此值:客户下达的订单的(order_num,total,date,product_name,quantity,price)。
我有4张桌子:
1)order(id,order_num,id_c,date,total)id_c是客户的外键
2)order_details(id,o_id,price,quantity,p_id)o_id是订单表的外键,而p_id是产品表的外键
3)产品(id,名称,价格)
4)customer(id,etc ...)
我用来获取值的查询是:
<?php
$sql=mysqli_query($con,"
SELECT o.order_num
, o.date
, o.total
, p.name
, od.price
, od.quantity
FROM customer c
JOIN order o
ON c.id = o.id_c
JOIN order_details od
ON o.id=od.o_id
JOIN product p
ON od.p_id = p.id"
) or die(mysqli_error($con));
?>
并打印结果:
<?php
while($row = mysqli_fetch_assoc($sql)){?> <td><?php
echo "Num. Ord.: " .$row['num']; echo "<br/>";
echo "Name prod: ".$row['name'];
echo "\n Price: ".$row['price'];
echo "\n Qt. ".$row['quantity']; echo "<br/>";
echo "Total: ".$row['total'];
echo "\n Date: ".$row['date'];echo "<br/>";
?></td><?php
}
?>
问题是:如果我订购多于1种产品,则所有值重复1次以上;如果我订购2种产品,则将值重复2次,如:
数字。顺序:1 产品名称: 价钱: ... Qt:... 总:.. 日期..
数字。 ord:1 产品名称:... 价钱: ... Qt:.. 总:.. 日期:..
即使订单号相同。即使订购了1种以上的产品,我怎么也只能打印1次订单?