PHP MySQL使用SELECT获取特定条目的SUM


PHP MySQL get SUM for only certain entries using SELECT

在下面的表格中,我将如何为特定年份(例如2013年)的价格总和编写一个select子句?比如SUM(price WHEN year = 2012)。我如何在PHP中引用mysql_fetch_array()。

+++++++++++++++++++++++++++++
Price | Date | Month | Year |
+++++++++++++++++++++++++++++
9.00  |343345|   2   | 2013 |
3.00  |343445|   2   | 2013 |
4.00  |343245|   1   | 2013 |
1.00  |342245|   1   | 2013 |
5.00  |333355|   12  | 2012 |

使用别名:

SELECT SUM(price) AS my_sum 
WHERE year = 2102

在php中:

$row = mysqli_fetch_assoc($result);
echo $row['my_sum'];

select sum(price) from table where year = 2012;

您可以使用:

$data = mysql_fetch_array($query);
echo $data[0];

对此的查询将如下所示:

select sum(price) as total_price from your_table_name where Year='2012';

在PHP中可以这样使用:

$getTotal = mysql_query("select sum(price) as total_price from your_table_name where   Year='2012'");
while($resTotal = mysql_fetch_array($getTotal))
{
 $total = $resTotal['total_price'];
}

现在你有了一个变量$total