Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplication of data from MySQL row

Tags:

php

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

like image 604
meandme Avatar asked Jan 26 '26 07:01

meandme


1 Answers

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

like image 59
ypercubeᵀᴹ Avatar answered Jan 27 '26 22:01

ypercubeᵀᴹ