douye1876 2014-02-27 16:56
浏览 26
已采纳

PHP算法和MySQL [关闭]

I have an equation that I need to process from a couple MySQL columns. I am just trying to figure out the best way of doing this once I run my SELECT statement to retrieve all the data.

Table - tbl_Spirits
Columns - ID, Volume, Proof

I know how to retrieve all the Data from MySQL, the problem is, is that there may be one record returned or 5. Probably never more then 5. Is there a clean fast method of the equation below in PHP.

This example is if there was 2 records returned.

((Volume1 * Proof1) + (Volume2 * Proof2)) / (Volume1 + Volume2) 
  • 写回答

1条回答 默认 最新

  • dongshi1188 2014-02-27 17:01
    关注

    Even if you did not show any attempt to solve your problem, I will give it a try. Basically you want to divide the sum of products between the two columns by the sum from one of them. So it should be something like

    SELECT 
        SUM(`Volume`*`Proof`)/SUM(`Volume`) 
    FROM 
        `tbl_Spirits` 
    WHERE 
        1 
    

    and you will need to fill in your WHERE statement to fit your needs

    Update: added this sample in sqlfiddle: http://sqlfiddle.com/#!2/0dc417/1/0 you will see that for the two records the value you will get is 4, exactly (2*4+3*4)/(2+3)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟