Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Grouping a Quantity Sum - MySQL or PHP?
Welcome, Guest. Please login or register.
November 21, 2008, 09:21:33 AM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Databases  |  MySQL  |  Grouping a Quantity Sum - MySQL or PHP? « previous next »
Pages: [1]
Author Topic: Grouping a Quantity Sum - MySQL or PHP?  (Read 348 times)
fedoracore

Offline Offline

Posts: 35



« on: March 15, 2007, 08:32:46 AM »

What is the "standard practice" for grouping multiple returns of the "same item" in a simple join query?
Please observe this lines from a working MySQL query Method:
Code:
$AGquery = "SELECT cat_id_ag, ag_id, ag_identity, ag_nickname, ag_details
FROM aguitar_inventory, aguitar_models
WHERE aguitar_inventory.ag_id = aguitar_models.ag_identity";

$AGresult = mysql_query($AGquery,$dblink) or die("I could not connect to the database because ".mysql_error());
$this->many=mysql_num_rows($AGresult);

for($z=0;$z<$this->many;$z++) {
$counter[$z]=  mysql_fetch_array($AGresult, MYSQL_ASSOC);
}
It's technically giving me what i'm asking for, but i don't want it to show all that it does in the ultimate "end-user view". Allow me to elaborate just a bit as it will probably cut-to-the-chase a bit faster: it's an on-line inventory for my friend's guitar store. I want the results to show one-of-each item
currently, any duplicate inventory item is printed as part of the $counter array...

...resulting in an output like:
$counter[0] = guitarA,
$counter[1] = guitarA,
$counter[3] = guitarA,
$counter[4] = guitarB

however, i want it to print to screen as
guitarA = qty 3
guitarB = qty 1
etc.

my question is:
do i want to handle the "sum" at MySQL (i.e. is there a MySQL built-in-syntax which will do such a thing? [MySQL v.5.0 - PHP 5.current]. i'm far from being well versed in the art of SQL, so please don't take for granted that i might know even the simplest of functions [i can do date INTERVALS and simple joins like above, and that's about it!]), or would i be best to try to create the "sum" on the PHP side? seems like a MySQL thing to me but it's just a hunch.

thanks!!!

PS. i also want to be able to report an "out-of-stock" item (at least in the 'backend'). hmm... i think i can tackle that it, but i'd appreciate any insight into a solution to the idea of maintaing an "active" inventory (ie. one which changes depending upon goods sold, and should reflect this status to prospective customers).
thank you!!
Logged

"...In search of my inner Joomla"
mishkad

Offline Offline

Posts: 15


« Reply #1 on: March 15, 2007, 11:56:14 AM »

Hi,
Try to add this:
Code:
$AGquery = "SELECT cat_id_ag, ag_id, ag_identity, ag_nickname, ag_details,count(ag_nickname)
FROM aguitar_inventory, aguitar_models
WHERE aguitar_inventory.ag_id = aguitar_models.ag_identity
Group by ag_nickname
Having count(ag_nickname)>1";

Assuming that ag_nickname is the field U look for (guitarTypo).

Regards,
M.D.
Logged
Pages: [1]
« previous next »
    Jump to: