Hi Dave,

> I am trying to count how many product names in my db have the same
> category id and then show it ie:
>
> Catid 1 Product 1
> Catid 1 Product 2
> Catid 2 Product 3
> Catid 3 Product 4
> Catid 3 Product 5
>
> Result would be
>
> Catid1 has 2 products
> Catid2 has 1 products
> Catid3 has 2 products
>
> I think it has something to do with the GROUP command but the mysql
doc
> dose not make it clear how to achive this task.
>
> Code examples, pointers to web resources or any info thankfully
> received.


Let's take it a step at a time. First of all assemble the SELECT to
produce your first list:

SELECT * FROM tblNm;

then pull in the GROUP BY clause to collect the row-results together in
some like-minded fashion. In this case you want to collect all or the
rows pertaining to one category (ID) together. (you will need to be more
specific about what in the manual is making you uncertain):

SELECT * FROM tblNm GROUP BY Catid1;

Oops! All of a sudden we only get one line for each CatId (and the rest
of the columns produce fairly unpredictable data taken from only one of
the rows with that CatId). Get rid of the * (all columns) and replace it
with the CatId colNm.

Now follow your instincts and check out COUNT() in the manual, and try
something like:

SELECT Catid1, count(*) FROM tblNm GROUP BY Catid1;

As I said 'follow your instincts' and take it one step at a time: Code
the simplest query first, then try making it more complicated by
adding/amending one clause at a time, crafting the result until it suits
your purposes...

Let us know how you get on!
=dn


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to