| | |
counting possible values of one column
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
do you mean something like this
SQL Syntax (Toggle Plain Text)
SELECT field2,count(Field1) FROM Table GROUP BY field2
Last edited by cgyrob; Jul 21st, 2009 at 2:14 pm.
Since you didn't give too much information I thought of a few other interpetations of what you asked.
If field1 is a numeric value you might want the sum of the field grouped by field2
or you might want to know how many records of field1 there are that are also rouped by field2
I'm sure one of the 3 syntax's will work for what you are asking.
If field1 is a numeric value you might want the sum of the field grouped by field2
sql Syntax (Toggle Plain Text)
SELECT field2, sum(field1) FROM Table GROUP BY field2
or you might want to know how many records of field1 there are that are also rouped by field2
sql Syntax (Toggle Plain Text)
SELECT field2, field1, count(*) FROM table GROUP BY field2, field1
I'm sure one of the 3 syntax's will work for what you are asking.
Last edited by cgyrob; Jul 21st, 2009 at 3:54 pm.
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
Thank you for replying. let me explain with an example 
let's say i have this table: (check attachment please)
i want to get a count for example like this :
CASE1:
OR CASE2:
in the first case the count returns possible values (cars) based on a condition (red cars)
Or in the second case the count returns how the number of different values assigned to the column "car" (as in how many is bmw and how many is audi...etc)
can this be done with a single SELECT query ?

let's say i have this table: (check attachment please)
i want to get a count for example like this :
CASE1:
•
•
•
•
cars|---------------|color red
bmw|---------------------------|1
audi|----------------------------|0
ford|-----------------------------| 1
toyota|---------------------------|0
•
•
•
•
cars|---------------|car count
bmw|----------------------------|3
audi|-----------------------------|2
ford|------------------------------| 1
toyota|--------------------------|1
Or in the second case the count returns how the number of different values assigned to the column "car" (as in how many is bmw and how many is audi...etc)
can this be done with a single SELECT query ?
Nevermind, I take it the attachment is the table schema.
Is it important to do it all in one query. What are you using the resultset for because if you do it in one query you will most likely get back something like the following.
BMW 2 RED 1
BMW 2 BLACK 1
AUDI 8 RED 3
AUDI 8 BLACK 2
AUDI 8 BLUE 3
etc....
Is it important to do it all in one query. What are you using the resultset for because if you do it in one query you will most likely get back something like the following.
BMW 2 RED 1
BMW 2 BLACK 1
AUDI 8 RED 3
AUDI 8 BLACK 2
AUDI 8 BLUE 3
etc....
Last edited by cgyrob; Jul 21st, 2009 at 7:14 pm.
Try this
sql Syntax (Toggle Plain Text)
SELECT MAKE.MAKE, MAKE.count, color.Color, color.count FROM( SELECT MAKE, count(*)count FROM cars GROUP BY MAKE )MAKE, ( SELECT MAKE, color, count(*)count FROM cars GROUP BY MAKE,color )color WHERE make.make = color.make
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
Try this
sql Syntax (Toggle Plain Text)
SELECT MAKE.MAKE, MAKE.count, color.Color, color.count FROM( SELECT MAKE, count(*)count FROM cars GROUP BY MAKE )MAKE, ( SELECT MAKE, color, count(*)count FROM cars GROUP BY MAKE,color )color WHERE make.make = color.make
although your code didn't quite work.. it did direct me to the right way in solving my problem so thanks you my friend
.problem solved.
added to your rep.
![]() |
Similar Threads
- Update the null values of a particular column (MS SQL)
- read a column of text from a file in java (Java)
- how to change values of particular column in datagrid... (VB.NET)
- assing table values to an array (JSP)
- Column number (Pascal and Delphi)
- order and then compare values from same column (Oracle)
- Hi,Need help on sql select statement structure (MS SQL)
- totals in column of a table (HTML and CSS)
- DataGrid - Column help needed (Visual Basic 4 / 5 / 6)
Other Threads in the MySQL Forum
- Previous Thread: Need some input
- Next Thread: Connect to MYSQL database on separate server with PHP
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enter eudora facebook form foss gartner gnu government greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





