473,218 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

MySQL Count() question.. (mysql newb)

Hi there,

I have a small question:
I have a table with lots of rows in it. Of course all have a different id,
but each can be assigned to a certain category. Categories correspond
with 1 - 10.

So a row looks like this: 'unique id | 4'

What i want is to do a count how much rows belong to a certain category.
Is it possible to do the following (from 1 query)

echo $counted_rows['4'];

Which sould return the number of rows with category '4';

Or is there anything similar if this is wrong?

Any help is great!! :)
Jul 17 '05 #1
6 4367
To get the number of rows with a category '4' use:

SELECT COUNT( * ) FROM `table` WHERE category = '4';

To get the row count for all categories in a single query (I think what
you're asking for):

SELECT COUNT( category ) AS repetitions, category FROM `table`
GROUP BY category

- Kevin

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
Hi there,

I have a small question:
I have a table with lots of rows in it. Of course all have a different id,
but each can be assigned to a certain category. Categories correspond
with 1 - 10.

So a row looks like this: 'unique id | 4'

What i want is to do a count how much rows belong to a certain category.
Is it possible to do the following (from 1 query)

echo $counted_rows['4'];

Which sould return the number of rows with category '4';

Or is there anything similar if this is wrong?

Any help is great!! :)

Jul 17 '05 #2
Well, to begin: Thank you for helping. :D

Ehm i know i am not asking for the first, because this would force me
to run the query for each category.

Could you please explain a little bit more about the second one? I can't seem
to get it to work... :( :s

Thanks again

"Kevin" <ke***@wxREMOVE4SPAM3.com> wrote in message news:<L8********************@comcast.com>...
To get the number of rows with a category '4' use:

SELECT COUNT( * ) FROM `table` WHERE category = '4';

To get the row count for all categories in a single query (I think what
you're asking for):

SELECT COUNT( category ) AS repetitions, category FROM `table`
GROUP BY category

- Kevin

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
Hi there,

I have a small question:
I have a table with lots of rows in it. Of course all have a different id,
but each can be assigned to a certain category. Categories correspond
with 1 - 10.

So a row looks like this: 'unique id | 4'

What i want is to do a count how much rows belong to a certain category.
Is it possible to do the following (from 1 query)

echo $counted_rows['4'];

Which sould return the number of rows with category '4';

Or is there anything similar if this is wrong?

Any help is great!! :)

Jul 17 '05 #3
What problem are you having? Obviously you need to replace the field and
table names with the ones from your table.

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
Well, to begin: Thank you for helping. :D

Ehm i know i am not asking for the first, because this would force me
to run the query for each category.

Could you please explain a little bit more about the second one? I can't
seem
to get it to work... :( :s

Thanks again

"Kevin" <ke***@wxREMOVE4SPAM3.com> wrote in message
news:<L8********************@comcast.com>...
To get the number of rows with a category '4' use:

SELECT COUNT( * ) FROM `table` WHERE category = '4';

To get the row count for all categories in a single query (I think what
you're asking for):

SELECT COUNT( category ) AS repetitions, category FROM `table`
GROUP BY category

- Kevin

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
> Hi there,
>
> I have a small question:
> I have a table with lots of rows in it. Of course all have a different
> id,
> but each can be assigned to a certain category. Categories correspond
> with 1 - 10.
>
> So a row looks like this: 'unique id | 4'
>
> What i want is to do a count how much rows belong to a certain
> category.
> Is it possible to do the following (from 1 query)
>
> echo $counted_rows['4'];
>
> Which sould return the number of rows with category '4';
>
> Or is there anything similar if this is wrong?
>
> Any help is great!! :)

Jul 17 '05 #4
Yes thanks, that far i got, but how can i call the values in different parts
of the page, whereever i want.
So for instance i have 10 tables and in the first one i call $counted_rows_4
or so, and in the next one $counted_rows_9. So they correspond with the
category. It doesnt matter how i call them, as long as i can call them
individually.

Hope you get it, and sorry for me being a noob. I googled on this, searched the
mysql site, but couldn't find anything that makes sense to me.

Thanks again.


"Kevin" <ke***@wxREMOVE4SPAM3.com> wrote in message news:<w4********************@comcast.com>...
What problem are you having? Obviously you need to replace the field and
table names with the ones from your table.

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
Well, to begin: Thank you for helping. :D

Ehm i know i am not asking for the first, because this would force me
to run the query for each category.

Could you please explain a little bit more about the second one? I can't
seem
to get it to work... :( :s

Thanks again

"Kevin" <ke***@wxREMOVE4SPAM3.com> wrote in message
news:<L8********************@comcast.com>...
To get the number of rows with a category '4' use:

SELECT COUNT( * ) FROM `table` WHERE category = '4';

To get the row count for all categories in a single query (I think what
you're asking for):

SELECT COUNT( category ) AS repetitions, category FROM `table`
GROUP BY category

- Kevin

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
> Hi there,
>
> I have a small question:
> I have a table with lots of rows in it. Of course all have a different
> id,
> but each can be assigned to a certain category. Categories correspond
> with 1 - 10.
>
> So a row looks like this: 'unique id | 4'
>
> What i want is to do a count how much rows belong to a certain
> category.
> Is it possible to do the following (from 1 query)
>
> echo $counted_rows['4'];
>
> Which sould return the number of rows with category '4';
>
> Or is there anything similar if this is wrong?
>
> Any help is great!! :)

Jul 17 '05 #5
Hi
I finally discover the way how to get what I need

Let's look to my code

----------------------- CODE ------------------------

$res = @mysql_query("SELECT COUNT(*) AS ToT FROM table_name");
$dato = mysql_fetch_array($res)

echo($dato[ToT])
----------------------- CODE ------------------------

With this code you will get how much records do you have in DB

PS
Whan you think that everything is over and that you are at bottom o
trying push harder to see what is under

Auro
http://eye.cc -php- web design
Jul 17 '05 #6
This gives you the record count for a single TABLE, not the entire DATABASE.
Learn to use the correct terminology.

--
Tony Marston

http://www.tonymarston.net

"auron" <ma***@compriovendi-dot-com.no-spam.invalid> wrote in message
news:kX*******************@news.sisna.com...
Hi,
I finally discover the way how to get what I need.

Let's look to my code:

----------------------- CODE ------------------------>

$res = @mysql_query("SELECT COUNT(*) AS ToT FROM table_name");
$dato = mysql_fetch_array($res);

echo($dato[ToT]);
----------------------- CODE ------------------------>

With this code you will get how much records do you have in DB.

PS:
Whan you think that everything is over and that you are at bottom of
trying push harder to see what is under.

Auron
http://eye.cc -php- web design

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xizor | last post by:
Ok, I'm new to PHP and MySQL. I've been going through tutorials, reading the documentation, and looking through web sites. PHP to me seems great! With MySQL it seems even better. However, I'm an...
2
by: marko2 | last post by:
I have debian running and this is how I have it setup # ls -ld /var/lib/mysql drwxr-xr-x 4 mysql mysql 4096 Aug 19 11:29 /var/lib/mysql I was shown how to setup a password for...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
14
by: dottty | last post by:
Hi, i have a table that has the following fields: id, name, dept, pay 1, John, Sales, 4000 2, Peter, HR, 5000 etc. How do i count how many people there are in each dept with an sql query? ...
5
by: NewbieSupreme | last post by:
I installed Apache 2.0.58, tested it, got the "working" page. Installed MySQL 5.0.22, and didn't really see how to test that, but there is a mysql process running in the task manager. Ran through...
8
by: Garry | last post by:
How do I cycle through a MySQL query result one row at a time so that I can do some work on each individual row, instead of having the whole query scroll by. I need to have the ability to post...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
12
by: Martien van Wanrooij | last post by:
I have been using for a while a class to create photo albums. Shortly, an array of photo's is declared, initially they are shown as thumbnails, by clicking on a thumbnail a larger photo is shown...
2
Paks
by: Paks | last post by:
Hello, I have a quick little question about the MySQL-command LEFT(columnName,int). My question is if there's a counterpart in PostgreSQL for this command and what it is? What I want to do is...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.