0

Given the following table:

type   |  valueOfType


food   |  4.3 

food   |  5.3

dairy  |  3.2

food   |  1.3

food   |  6.7

==============

My query needs to search for all "food" type and add all the "valueOfType" where valueOfType is a float type. How can I do it?

edit:

$query = "SELECT SUM(valueOfType) AS Total FROM productTbl WHERE type ='$post_type' ";
$result = mysql_query($query2) 
or die("Unable to execute query because : " .     mysql_error());

echo $result;     
2
  • 1
    Did you try writing DB query and code afterwards? Commented Mar 7, 2012 at 4:12
  • use SUM() function as said by judda Commented Mar 7, 2012 at 4:16

1 Answer 1

5
SELECT SUM(valueOfType) AS Total FROM foo WHERE type='food'
Sign up to request clarification or add additional context in comments.

5 Comments

how do I retrieve the answer of the query? Sorry Im very new at PHP. I did exactly, but when I echo-ed the answer, it gave me Resource id #3.
Using the mysql_query or whatever functions you are using to pull information from your database. Tutorial: tizag.com/mysqlTutorial
hi, sorry to bother you again, can you look at the edited question? I've added the coding. Sorry!
Did you read the tutorial link that I posted? You need to get the data into an array before emitting it. $foo = mysql_fetch_assoc($result); echo $foo['Total'];
Oh crap, I didn't notice the tutorial part. Sorry, and thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.