繁体   English   中英

PHP MySQL使用SELECT仅对某些条目获取SUM

[英]PHP MySQL get SUM for only certain entries using SELECT

在下表中,我将如何为特定年份(例如2013年)的价格总和编写一个select子句? 例如SUM(当年份= 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的总数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM