如何查找具有数量值的两列的总和

问题描述:

Ok so I am a lil confused how to go about this and seek some help..

I have a mysql db that has a column named quantity and another called price_paid

what I want to do is take and add up the entire column of price_paid but to also take into account a quantity higher than 1.

Ex..

_____________     ______________   
|  quantity  |   | price_paid  | 
--------------   ---------------
       1              2.99 
       2              9.99
       1              1.99

How would I create the php / mysql to make that = a total of 24.96 ?

So far I am able to get the total of price_paid but what I have doesn't take into account the quantity column and it's value.

$comic_totals = "SELECT sum(price_paid) AS value_sum FROM `comic_db`.`comic_db`";
$total_db = mysqli_query($comic_connect, $comic_totals);
if(!total_db){
        echo "Failed";
}
$tot_array = mysqli_fetch_assoc($total_db);
$sum = $tot_array['value_sum'];
echo "$".$sum;

so how do I turn that into what I need? Thanks

好的,所以我很困惑如何解决这个问题并寻求帮助.. p>

我有一个mysql数据库,其中有一个名为quantity的列,另一个名为price_paid p>

我想要做的就是拿出并添加整个price_paid列,但也要采取 考虑到高于1的数量。 p>

Ex .. p>

  _____________ ______________ 
 | 数量|  |  price_paid |  
 -------------- --------------- 
 1 2.99 
 2 9.99 
 1 1.99 
  code>  
 
 

我如何创建php / mysql以使其=总共24.96? p>

到目前为止,我能够获得price_paid的总数,但我所拥有的并没有考虑数量列及其值。 p>

  $ comic_totals =“SELECT sum(price_paid)AS value_sum FROM` comic_db` .comic_db`”; 
 $ total_db = mysqli_query($ comic_connect,$ comic_totals); \  nif(!total_db){
 echo“Failed”; 
} 
 $ tot_array = mysqli_fetch_assoc($ total_db); 
 $ sum = $ tot_array ['value_sum']; 
echo“$”。$ sum; \  n  code>  pre> 
 
 

那么如何将其转化为我需要的? 谢谢 p> div>

Let MySQL take care of multiplying the price by the quantity...

SELECT SUM(quantity * price_paid) AS value_sum

If you want quantity higher than 1 then add where clause

Like this

SELCET SUM(quantity * price_paid) as value
FROM comic_db.comic_db
WHERE quantity > 1;