Part of the EllisLab Network
   
 
MySQL query for TOP10 rated
Posted: 18 November 2006 07:38 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  80
Joined  07-06-2006

Hi!

I’m wondering if someone can help me with a MySQL query for getting the top 10 best rated movies for an app I’m working on.

The DB fields are:

movie_id
movie_title
user_id
points

I’m trying with this:

$query = $this->db->query("SELECT MAX(SUM(points)/COUNT(user_id)) FROM movies LIMIT 10");
        foreach (
$query->result() as $row)
        
{
        
echo $row->movie_title;
        
}

but is not working, it shows the this error:

An Error Was Encountered

Error Number: 1111

Invalid use of group function

SELECT MAX(SUM(points)/COUNT(user_id)) FROM peliculas LIMIT 10


Can someone help me with this query? Thx

 Signature 

Guido Rossi

Profile
 
 
Posted: 18 November 2006 07:49 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  313
Joined  07-23-2006

try adding a GROUP BY movie_id

 Signature 

running man webdesign

Profile
 
 
Posted: 18 November 2006 07:54 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  80
Joined  07-06-2006

try adding a GROUP BY movie_id

is still the same error

and using:

SELECT SUM(points)/COUNT(user_id) as average FROM movies ORDER BY COUNT(user_id) desc LIMIT 10

I’m getting this error:

A PHP Error was encountered

Severity: Notice

Message: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.

Filename: libraries/Loader.php

Line Number: 648

 Signature 

Guido Rossi

Profile
 
 
Posted: 18 November 2006 08:47 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  172
Joined  04-15-2006

This should work:

SELECT movie_id, (SUM(points) / COUNT(*)) AS average
FROM movies
GROUP BY movie_id
ORDER BY average DESC
LIMIT 10
;

 Signature 

Prezent - CI website with best gift ideas from Polish e-shops.

Profile
 
 
Posted: 18 November 2006 08:53 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  80
Joined  07-06-2006
Bart - 18 November 2006 08:47 PM

This should work:

SELECT movie_id, (SUM(points) / COUNT(*)) AS average
FROM movies
GROUP BY movie_id
ORDER BY average DESC
LIMIT 10
;

with that query I’m getting this same error:

A PHP Error was encountered

Severity: Notice

Message: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.

Filename: libraries/Loader.php

Line Number: 648

I think it can be a CI problem

 Signature 

Guido Rossi

Profile
 
 
Posted: 18 November 2006 08:59 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  172
Joined  04-15-2006

Yes, that is not an SQL error.

 Signature 

Prezent - CI website with best gift ideas from Polish e-shops.

Profile
 
 
Posted: 19 November 2006 06:36 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

nooby, your SELECT isn’t returning movie_title, so you should be getting an error along the lines of “$row->movie_title is undefined”.
If fixing your SELECT gets your desired result, then we’re in similar territory to this thread, whereby a bad or missing variable doesn’t generate the expected error type.

Profile
 
 
Posted: 19 November 2006 09:42 AM   [ Ignore ]   [ # 7 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

I responded to a post you made in the bug forum.  I think you’re seeing “ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.”  Because you have compress_output set to true.  There actually is another error in there, but the compress output is getting triggered first.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 19 November 2006 10:17 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  80
Joined  07-06-2006
Martin Hall - 19 November 2006 06:36 AM

nooby, your SELECT isn’t returning movie_title, so you should be getting an error along the lines of “$row->movie_title is undefined”.
If fixing your SELECT gets your desired result, then we’re in similar territory to this thread, whereby a bad or missing variable doesn’t generate the expected error type.

Why you think isn’t returning movie_title? The error is in the query, if I change the query to something simple like “SELECT * FROM peliculas LIMIT 10” it works OK


[quote author=“Derek Allard”]I responded to a post you made in the bug forum.  I think you’re seeing “ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.” Because you have compress_output set to true.  There actually is another error in there, but the compress output is getting triggered first.

$config[‘compress_output’] is setted to false, so the error is not because of that, I think is because CI can not handle that kind of query

 Signature 

Guido Rossi

Profile
 
 
Posted: 19 November 2006 10:26 AM   [ Ignore ]   [ # 9 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

Why you think isn’t returning movie_title? The error is in the query, if I change the query to something simple like “SELECT * FROM peliculas LIMIT 10” it works OK

Actually, Martin seems to be right, based on what you posted.  Here is your SQL

SELECT MAX(SUM(points)/COUNT(user_id)) FROM movies LIMIT 10

notice that movie_title isn’t in there?  What if you make it

SELECT movie_title, MAX(SUM(points)/COUNT(user_id)) FROM movies LIMIT 10

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 19 November 2006 10:35 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  80
Joined  07-06-2006

Oh you’re right! That’s the error in the SQL query!

Thank you very much for helping!

 Signature 

Guido Rossi

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66412 Total Logged-in Users: 24
Total Topics: 84753 Total Anonymous Users: 1
Total Replies: 454814 Total Guests: 241
Total Posts: 539567    
Members ( View Memberlist )
Newest Members:  NirCalexmuellerkizerdrixcaptainredmuffquinodligtharttechsivamDjordjesammozzazodman23