473,289 Members | 2,087 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,289 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 4370
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.