truegilly Posted April 7, 2007 Share Posted April 7, 2007 Hi Guys Im using MYSQL with PHP and normally if i need to combine the outputs, create multiple MYSQL query's and add the results together. I was wondering if it is possible using a single query to count different data thats in the same row of a table. The example below counts the number of times green in in the Actual_Colour field. mysql> SELECT COUNT(*) FROM tornado_node WHERE Actual_Colour = 'green'; +----------+ | COUNT(*) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec) All very simple. If i wanted to produce the same thing that also counted red and blue values that are in the Actual_Colour field do i have to create multiple querys or can it be done on a single line of SQL code? ??? what i was hoping was to produce an output like this this below... +----------++----------++----------+ | Red || green || Blue | +----------++----------++----------+ | 5 || 6 || 2 | +----------++----------++----------+ i could then in PHP set this as an associated array. thank you for any help Truegilly Quote Link to comment https://forums.phpfreaks.com/topic/46059-multipule-counts-in-one-query/ Share on other sites More sharing options...
fenway Posted April 7, 2007 Share Posted April 7, 2007 Are you set on this being a single record? Quote Link to comment https://forums.phpfreaks.com/topic/46059-multipule-counts-in-one-query/#findComment-223848 Share on other sites More sharing options...
truegilly Posted April 8, 2007 Author Share Posted April 8, 2007 Hi Thanks for you reply, basically i was just thinking that having one query would be more efficient. Is this true ? ??? thanks Truegilly Quote Link to comment https://forums.phpfreaks.com/topic/46059-multipule-counts-in-one-query/#findComment-223884 Share on other sites More sharing options...
fenway Posted April 8, 2007 Share Posted April 8, 2007 Because a simple count() with group by will get back one record per colour... Quote Link to comment https://forums.phpfreaks.com/topic/46059-multipule-counts-in-one-query/#findComment-224430 Share on other sites More sharing options...
gluck Posted April 10, 2007 Share Posted April 10, 2007 Try Group By with the color column you should get all results in one query .. The result will be one row for red, next for green etc. Quote Link to comment https://forums.phpfreaks.com/topic/46059-multipule-counts-in-one-query/#findComment-226379 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.