douduikai0562 2014-05-05 09:01
浏览 232
已采纳

如何计算php中列的平均值

I have a database from which I call data according to checkboxes checked(Retrieve data from sql database and display in tables - Display certain data according to checkboxes checked). I want to add an option of calculating their average (Between the wanted dates). How can I write a query, calculating the averages when the output is not something specific?

This is the query that calls my data at first place:

$tmp ="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\"";

How can the average-query call the data according to checkboxes checked + don't average the date and hour column:

$average="SELECT AVG(",", $sql_columns) AS PriceAverage FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 

Here is my attempt but don't seem to work. The problem is that the AVG is for strings and I want to display an array.

  • 写回答

1条回答 默认 最新

  • douxi3085 2014-06-03 08:02
    关注

    Firstly you state a variable that matches your columns [inside the parenthesis e.g (DNI)] and give the command to calculate their average. Finally name the average e.g 'adni'.

    $avg_names=array('1'=>'AVG(DNI) as adni', '2'=>'AVG(GHI) as aghi', '3'=>'AVG(Zenith_Angle) as azenith', '4'=>'AVG(DiffuseHI) as adhi');
    
    $average="SELECT ".implode(",", $avg_names)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测