Craig79 Posted January 11, 2009 Share Posted January 11, 2009 Hello, I have a column of integers that are all multiples of 5. How do I determine which number in the field occurs most frequently? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/140375-solved-need-help-determining-most-frequent-number-in-a-field/ Share on other sites More sharing options...
Gamic Posted January 11, 2009 Share Posted January 11, 2009 select * from( select numberField, count(*) as countOfUse from table group by number )tmp order by tmp.countOfUse desc Quote Link to comment https://forums.phpfreaks.com/topic/140375-solved-need-help-determining-most-frequent-number-in-a-field/#findComment-734606 Share on other sites More sharing options...
Craig79 Posted January 11, 2009 Author Share Posted January 11, 2009 Thanks Gamic, I've never seen a query like that! I tried it but I couldn't get it to work. Here's what I did: $SQL = "select * from(select my_state, count(*) as countOfUse from t_locations group by my_price) tmp order by tmp.countOfUse desc"; The "my_state" field in the "t_locations" table consists of integers. These integers stand for USA state codes. The "my_price" field is in the same table and it also consists of integers (dollar values rounded up). I'm passing in the state code from a PHP script to find the most commonly paid price for that state (but not the average). So, the puzzle is to find the most commonly paid price where the state code = 65. Could you please clarify the query? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/140375-solved-need-help-determining-most-frequent-number-in-a-field/#findComment-734612 Share on other sites More sharing options...
Gamic Posted January 11, 2009 Share Posted January 11, 2009 <?php $SQL = " select * from( select my_state, count(*) as countOfUse from t_locations group by my_state )tmp order by tmp.countOfUse desc"; ?> This query tells you how many times a value of my_state appears in t_locations. To get the most commonly paid price for a state you would do something similar. selet * from( select my_price, count(*) as countOfUse from t_locations where my_state = 65 group by my_price )tmp order by tmp.countOfUse desc Quote Link to comment https://forums.phpfreaks.com/topic/140375-solved-need-help-determining-most-frequent-number-in-a-field/#findComment-734660 Share on other sites More sharing options...
Craig79 Posted January 11, 2009 Author Share Posted January 11, 2009 That's it! I tried researching how to do it but didn't see anything like that. Thank you Gamic! Quote Link to comment https://forums.phpfreaks.com/topic/140375-solved-need-help-determining-most-frequent-number-in-a-field/#findComment-734875 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.