douzhanglu4591 2011-12-16 19:21
浏览 792
已采纳

MySQL行数据的乘法

I would like to be able to multiply all the prices for example from my database then multiply the total by 10.

The code below do the addition not multiplication

<?php
$d = $_GET['d'];
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("databasename", $con);//

$sql= "SELECT SUM(prices) FROM tablename WHERE Date = '$d' AND Prices >0.20 AND Type= 'sold'";
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
echo $row[0]*10;


mysql_close($con);
?> 

example

id prices
1   25
2   36
3   45

That is i want 25*36*45*10 and not 25+36+45*10

Hope anyone can help

  • 写回答

2条回答 默认 最新

  • douchi2022 2011-12-16 20:13
    关注

    This would work, with restrictions (numbers should be only positive):

    SELECT 10 * EXP(SUM(LOG(prices))) AS result
    FROM tablename 
    WHERE ...
    

    The thnx should go to Napier and his wonderful invention, logarithms

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog