Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

php - Multiplication of data from MySQL row

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...