/    Sign up×
Community /Pin to ProfileBookmark

MYSql Summ function?

Is there a quick way to total values in a mySql database table? Say I want to look @ the total inventory of a product category, rather than reading through each item and adding together, is there 1 command?

I looked @ mysql.com with no avail.

Thanks in advance.

DPK

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@chazzyOct 26.2005 — could you show us some of your table structure? usually you want to select sum(col_name),other_column from table_name group by other_column
Copy linkTweet thisAlerts:
@dpkyteauthorOct 26.2005 — product_code,

product_group

inventory

bin_nbr

///////

Want one select to summ the inventory, where product_group = 'x'

Thanks
Copy linkTweet thisAlerts:
@SheldonOct 26.2005 — [code=php] $count = "SELECT count(inventory),product_group FROM ***** Where product_group = {$_POST['xx']}";
$total = mysql_query($count);


//print your values
echo "Total : {$total['inventory']} in {$total['product_group']}";


[/code]

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Copy linkTweet thisAlerts:
@chazzyOct 26.2005 — sheldon:

replace your count with sum and it's correct. count will give you the number of records matching each product_group. sum(inventory) calculates the total amount in each row of the column inventory matching product_group.
Copy linkTweet thisAlerts:
@SheldonOct 26.2005 — Ah, thats the first time i have used it, i thought it would be a good brain teaser. Thanks
Copy linkTweet thisAlerts:
@dpkyteauthorOct 26.2005 — Thanks, I realized I had to use SUM, not count. Thanks for directing me in the proper place.

I ended up going with:

$count = "SELECT sum(inventory) AS tot_inv FROM inv WHERE product_code = '$prod_code'";

$result = mysql_query($count);

$row = mysql_fetch_assoc($result);

$total = $row["tot_inv"];

echo $total;

Thanks all!!
×

Success!

Help @dpkyte spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...