Page 1 of 1

Counting for the same manufacturer...

Posted: Mon Jul 05, 2004 1:38 am
by kevin7
id manufacturer
1 nokia
2 sony ericsson
3 nokia
4 nokia
5 sony ericsson
6 motorola
7 sony ericsson
8 nokia
9 nokia
10 motorola
11 motorola
12 sony ericsson

question is, how can i return the result like the following:

nokia=5
sony erisson=4
motorola=3

i wanna get the total number of all the manufacturers that having the same name in a table... group 'em together and return the total...

tq!

Posted: Mon Jul 05, 2004 2:52 am
by ol4pr0

Code: Select all

$result = mysql_query("SELECT * FROM table WHERE manufactures='nokia'");
$num_rows = mysql_num_rows($result);
echo 'nokia='.$num_rows.'';
Did you mean that ?

Posted: Mon Jul 05, 2004 3:17 am
by feyd
If you store the manufacturer name in the phones table: this could shed some light.

If you store the id, or you have a seperate table with all the manufacturers:

Code: Select all

SELECT count( m.`id` ) `count`, m.`name`, m.`id` 
FROM  `manufacturers` m 
LEFT  JOIN  `phones` p ON m.`id`  = p.`manufaturer`
GROUP  BY m.`id`

Posted: Mon Jul 05, 2004 5:34 am
by redmonkey

Code: Select all

SELECT manufacturer, COUNT(*) as count
FROM manufacturers GROUP BY manufacturer;

Posted: Mon Jul 05, 2004 12:21 pm
by kevin7
ya~ redmonkey!! this is what i want!, u get exactly what i means... tq