我的问题有点长......
这是我提问的第一张照片:
---这是我如何进行选择的查询:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
order by product_id desc";
$result_8=mysql_query($query_8);
---如您所见,选择表需要按查询分组,这是查询和结果:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
group by product_id desc";
$result_8=mysql_query($query_8);
---这是我获得SUM的方式:
$query_9="select SUM(totali) as totali from orders where costumer_name='$costumer_name' && date='$date'";
$result_9=mysql_query($query_9);
$row_9=mysql_fetch_array($result_9);
$totali=$row_9['totali'];
---最后问题是:
1.如何计算具有相同product_id(sasia)的订单数量? 2.获得-cmimi和-sasia的第一个总数? 3.获得最终总数?
谢谢
答案 0 :(得分:1)
首先,我认为有更好的解决方案然后每当有人点击添加到购物车按钮以始终在数据库中插入新行。如果产品已经存在,那么您应该将数量更新为+1,这样您将只有一行产品而不是3.我不确切知道您的数据库看起来如何但这里有一个想法如何做到这一点:< / p>
CREATE PROCEDURE add_to_cart(IN in_cart_id INT, IN in_product_id...)
BEGIN
DECLARE quant INT;
SELECT quantity
FROM cart
WHERE cart_id = in_cart_id
AND product_id = in_product_id
INTO quant;
IF quant IS NULL then
INSERT INTO cart....
ELSE
UPDATE cart
SET quantity = quantity + 1
WHERE cart_id = in_cart_id
AND product_id = in_product_id;
END IF;
END$$
我认为这是更好的解决方案,但如果你想以你开始的方式做到这一点,那么这是一种方法:
SELECT productId, name, price, COUNT(quantity) as quantity, (price * COUNT(quantity)) as total
FROM where-ever-you store data
WHERE expression
GROUP BY productId;
这是对你的两个问题的答案,对于你需要编写单独的SELECT语句的第三个总和
SELECT SUM(price * quantity) as total_sum
FROM where-ever-you-store-data
WHERE expression if needed...