473,581 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting number of matches with one database query?

Hi,

I'm new to PHP and I have a question that I think has a simple answer
but I just can't figure it out so I'm really hoping that one of you
gurus can help me out.

I'm trying to return data from mysql database. I have over a thousand
records and each record has a "type" field with values ranging from 1
to 9.

I'm setting up an html table that has 9 cells and all I want to do is
count the total amount of types. For example, there are 140 records as
type 1, 300 records as type 2 etc.

Do I need to do 9 database querys or can this be done with one query?

Any and all help is appreciated!

Jul 17 '05 #1
2 1462
>I'm new to PHP and I have a question that I think has a simple answer
but I just can't figure it out so I'm really hoping that one of you
gurus can help me out.

I'm trying to return data from mysql database. I have over a thousand
records and each record has a "type" field with values ranging from 1
to 9.

I'm setting up an html table that has 9 cells and all I want to do is
count the total amount of types. For example, there are 140 records as
type 1, 300 records as type 2 etc.

Do I need to do 9 database querys or can this be done with one query?


select type, count(type) from mytable group by type order by type;

This has the advantage or disadvantage, depending on how you look at it,
of not returning a row for any type with NO records for it, and it also
returns rows for type 11 which is actually a mistype for type 1.

Another variant of this, good for "top 10" lists:

select type, count(type) as cnt from mytable group by type order
by cnt limit 10;

Gordon L. Burditt
Jul 17 '05 #2
Great help, thanks a lot!

Jul 17 '05 #3

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

Similar topics

5
2232
by: Adam i Agnieszka Gasiorowski FNORD | last post by:
I need help width formulating the most effective (in terms of processing time) SQL query to count all the "new" documents in the repository, where "new" is defined as "from 00:00:01 up to 23:59:59 today". My current query does not give me satisfactory results, it creates a visible delay in rendering of the main page of one of the...
3
1346
by: Chris | last post by:
Hi, This is really more of a MySQL question than PHP per se, but the only MySQL group my newserver carries seems to be pretty quiet, so I thought I'd ask here. What I am trying to do is determine the number of terms which match a field in the database where an initial string of multiple words has been broken up into single terms and the...
1
1888
by: Sourabh | last post by:
Hello, I am trying to write a VB.NET monitoring application for a MS SQL server. For that I need to know the following: 1. How do I count the number of read/write accesses per database on the server since the creation of the DB? 2. How do I get the last access time of the database ? Any inputs would be really appreciated.
8
1779
by: mgm | last post by:
hello, I have a query that is supposed to return only 1 record, however I recently found that because of an error in the database it can return more than 1. So what I need to do is capture if it is more than 1 record and alert the user to contact the database administrator. How can I get the count of records returned by a recordset? Is...
2
3118
by: Glenn Cornish | last post by:
I have 5 fields in a table into which numbers between 1 and 45 can be entered. What I am having trouble with is being able to find out is how many times a particular number appears, regardless of which of the 5 fields it was entered into. Can anyone suggest a remedy for this. Many Thanks Glenn
3
1955
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let me give you a little background. this database is kind of like a human resources database. it collects info about people, where they work, and if...
5
3501
by: ChadDiesel | last post by:
Hello Again, I want to assign a number to each record that will be part of a shipping number. I want the number value to count up until the contract number changes. Then, I want the number to go back to 1 and start counting up again until the next contract change. For example Contract 1111111 Box 1 of 2 Number Value: 1
18
2928
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other employee knows anything about Access. I've searched Google Groups, and that has been a lot of help, but there are some questions that I just can't find...
1
5410
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the database so I could sort the records by the date at which they were entered. Well now I've deleted some of those records so its of course causing...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.