Rohlan Posted December 17, 2010 Share Posted December 17, 2010 Hello all. I'm having a difficult time figuring out this query, I even think I've done it once before but can't seem to remember what I did. This is my data: +--+----------+ |ID | type | +--+----------+ | 1 | type_1 | | 2 | type_2 | | 3 | type_2 | | 4 | type_1 | | 5 | type_1 | +--+----------+ I simply need to get the results in this fashion: Types | Quantity type_1 | 3 type_2 | 2 Why am I not figuring this out? seems so simple.. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/221972-semi-complex-query-that-counts-number-of-occurrences-of-a-certain-value/ Share on other sites More sharing options...
trq Posted December 17, 2010 Share Posted December 17, 2010 SELECT type AS Types, COUNT(ID) AS Quantity FROM tablename GROUP BY type; Quote Link to comment https://forums.phpfreaks.com/topic/221972-semi-complex-query-that-counts-number-of-occurrences-of-a-certain-value/#findComment-1148593 Share on other sites More sharing options...
Rohlan Posted December 17, 2010 Author Share Posted December 17, 2010 ... obviously was drawing a COMPLETE BLANK. I was forgetting the GROUP BY. Here's my query ... SELECT type, COUNT(*) AS Types FROM data_table WHERE type IS NOT NULL GROUP BY type Quote Link to comment https://forums.phpfreaks.com/topic/221972-semi-complex-query-that-counts-number-of-occurrences-of-a-certain-value/#findComment-1148595 Share on other sites More sharing options...
Rohlan Posted December 17, 2010 Author Share Posted December 17, 2010 oh, we posted almost at the same time, hehe thank you very much for the help Quote Link to comment https://forums.phpfreaks.com/topic/221972-semi-complex-query-that-counts-number-of-occurrences-of-a-certain-value/#findComment-1148597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.