markmuir Posted August 30, 2006 Share Posted August 30, 2006 Hi guys,Having a minor problem here. On my reports page, I want to display the top 5 books that people have chose.In my database, i have 3 fields (Book, School, Hits). Inside my insert.php script, it automatically inserts a number 1 into the hits every time I submit the form.On my reports page, I am able to list the books with the amount of hits they have had but I'm unable get them to display the highest first.[i]insert.php[/i]<?$link = mysql_connect("localhost", "username", "password") or die("Could not connect");$db = mysql_select_db("database", $link) or die("Could not select database");$book=$_POST['book'];$school=$_POST['school'];$result=mysql_query("INSERT INTO `books` VALUES ( '$book', '$school', '1') ");?>[i]reports.php[/i]<?$link = mysql_connect("localhost", "username", "password") or die ('My SQL Error: ' . mysql_error());$db = mysql_select_db("database");$result = mysql_query ( "SELECT Book, sum(Hits) FROM `books` GROUP BY 'Book' ") or die("SELECT Error: ".mysql_error());?><? print "<table width=400 border=3>\n";while ($get_info = mysql_fetch_row($result)){ print "<tr>\n";foreach ($get_info as $field) print "\t<td><font face=verdana size=3/>$field</font></td>\n";print "</tr>\n";}print "</table>\n";?> Link to comment https://forums.phpfreaks.com/topic/19163-sum-order-by-query/ Share on other sites More sharing options...
fenway Posted August 30, 2006 Share Posted August 30, 2006 Just use a column alias for the SUM() and then order by that desc. Link to comment https://forums.phpfreaks.com/topic/19163-sum-order-by-query/#findComment-82987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.