lilywong Posted August 3, 2006 Share Posted August 3, 2006 i have a table data as below:[b]Item[/b]pepsipepsiorangemiloorangemilomiloorangepepsipepsipepsii want to count and get the maximum data, which is pepsi in this case, how should i do in mysql Quote Link to comment Share on other sites More sharing options...
yarub Posted August 3, 2006 Share Posted August 3, 2006 $i=0;$query = mysql_query("SELECT COUNT(Item) FROM tablename WHERE Item='Pepsi'");while($row = mysql_fetch_array($query)) {$i++;echo("Total Pepsi: ".$row['COUNT(Item)']."");}Would appear as...Total Pepsi: 5 Quote Link to comment Share on other sites More sharing options...
lilywong Posted August 3, 2006 Author Share Posted August 3, 2006 i wish to get the result as pepsi, for example, i want to select the top selling drink in the shop, so i need to query to return me pepsi, coz pepsi appear 5 times. thanks. Quote Link to comment Share on other sites More sharing options...
manmadareddy Posted August 3, 2006 Share Posted August 3, 2006 $sql="select distinct(Item),count(Item) as cnt,itemcode from products group by Item";By using the above query u will get the counts of each item with itemcode.u can do sort on the result set and get the max one. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 3, 2006 Share Posted August 3, 2006 You don't need the distinct, nor will it have any effect with the GROUP BY already in place. DISTINCT is NOT a function! Quote Link to comment 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.