stelhar_1999 Posted February 23, 2006 Share Posted February 23, 2006 i want to make a top 10 using a field containing hits and show up a percentage in the resultsWhat i try to do is to SUM the top 10 hits using a query like this:"SELECT SUM(hits) as sumvotes ORDER BY sumvotes DESC LIMIT 10"but The number that I get is the sum of all the hits in the db and not for the top 10where am I wrong? Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2006 Share Posted February 23, 2006 [!--quoteo(post=348660:date=Feb 23 2006, 07:31 AM:name=stelhar_1999)--][div class=\'quotetop\']QUOTE(stelhar_1999 @ Feb 23 2006, 07:31 AM) [snapback]348660[/snapback][/div][div class=\'quotemain\'][!--quotec--]i want to make a top 10 using a field containing hits and show up a percentage in the resultsWhat i try to do is to SUM the top 10 hits using a query like this:"SELECT SUM(hits) as sumvotes ORDER BY sumvotes DESC LIMIT 10"but The number that I get is the sum of all the hits in the db and not for the top 10where am I wrong?[/quote]well, to keep an accurate count, you're having to keep track of each hit and which page it is from, right? well, in your query, you have to distinguish between the hits by using a GROUP BY clause. so, if you have a column called "page_id" where you record the page that is hit, you would do something like this:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] page_id, [color=blue]SUM[/color](hits) [color=green]AS[/color] sumvotes GROUP BY page_id [color=green]ORDER BY[/color] sumvotes [color=green]DESC[/color] LIMIT 10;[!--sql2--][/div][!--sql3--]hope this helps. Quote Link to comment Share on other sites More sharing options...
stelhar_1999 Posted March 1, 2006 Author Share Posted March 1, 2006 every entry in the db is unigue so there is no need for a GROUP BY, I think that is!!! Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 1, 2006 Share Posted March 1, 2006 I think what you may need to do is use a loop for unique pages$query=mysql_query("SELECT UNIQUE(page_id) FROM table");while ($row = mysql_fetch_array($query)) {$query2=mysql_query("SELECT count(hits) as sumhits FROM table WHERE page_id = '$row[page_id]'");$row2=mysql_fetch_array($query2);echo("$row[page_id] = $row2[sumhits]<br>");}That would get all of the pages and display how many hits they got each.. the only way i can see being able to get it to order the top 10 is to use a table with just page_id and hits as the coloumns and then truncate it before use and then insert all pages and hit counts then runa simple TOP query on it.. Quote Link to comment Share on other sites More sharing options...
stelhar_1999 Posted March 1, 2006 Author Share Posted March 1, 2006 to be more precise here is the structure of my table "data"[u]id|radioname |blabla|....|blabla|hits[/u]1 |testradio|blabla|....|blabla|200-------------------------------------------2 | testradio2|blabla|....|blabla|7So every entry is unigue and I want to have the top 10 radios, showing their percentage instead of hits!The problem is that I can't take the sum of the top 10 radios with a query! 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.