Jump to content

SUM & ORDER BY query


markmuir

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.