总结列中的所有数量

时间:2015-06-06 07:47:17

标签: php mysql mysqli

我的表格中有void foo (int a , int c) { d = a + c; } 列,我想在变量中总结此列中的所有qty

qty

6 个答案:

答案 0 :(得分:2)

试试这个。

$get_sold = "select sum(`qty`) as sum from pending_orders where product_id='$p_id' AND order_status='done'";

$result = mysqli_query($con,$get_sold);

$行= mysqli_fetch_assoc($结果); echo $ row [' sum'];

答案 1 :(得分:0)

尝试使用MySql sum函数

$get_sold = "select sum(qty) total_qty from pending_orders where product_id='$p_id' AND order_status='done'";

$result = mysqli_query($con,$get_sold);

答案 2 :(得分:0)

你应该使用 从pending_orders中选择 sum(qty)作为sumqty ,其中product_id =' $ p_id' AND order_status ='已完成'

还要提一下,在mysql中你需要写上等号而不是两个

答案 3 :(得分:0)

大多数人都在这里回复了一个快速复制/粘贴,但我觉得它更适合格式化您的查询以便于阅读以帮助您识别问题。

$get_sold = "
    SELECT SUM(`qty`) as `total_quantity`
    FROM `pending_orders`
    WHERE `product_id` = '$p_id' AND `order_status` = 'done'
";

$result = $con->query($get_sold);

if ( !$result ) {
    die("MySQL error: " . $con->error);
}

$row = $result->fetch_assoc();
$quantity = $row['total_quantity'];

答案 4 :(得分:0)

首先,您需要使用sum(qty)as totals对查询中的列求和,并从== order_status删除= <{1}}

您需要使用mysqli_fetch_row将汇总数据输入变量

<?php
$get_sold = "select sum(`qty`)as totals from `pending_orders` where `product_id`='".$p_id."' AND `order_status`='done'";

$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_row($result);
$row[0];// get your total sum

答案 5 :(得分:-1)

CHANE QUERY,SUM(column_name)给出column_name

中所有数字的总和
$get_sold = "select SUM(qty) as total from pending_orders where product_id='$p_id' AND order_status='done'";