Counting for the same manufacturer...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Counting for the same manufacturer...

Post 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!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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 ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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`
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

SELECT manufacturer, COUNT(*) as count
FROM manufacturers GROUP BY manufacturer;
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

ya~ redmonkey!! this is what i want!, u get exactly what i means... tq
Post Reply