mysql - What is wrong with this SQL -


i have following sql query:

select tblbooks.bookid,     tblbooks.title,     tblbooks.author,     tblbooks.coverimage,     round(round(tblreviews.rating * 2) / 2, 1) rating tblbooks     left join tblreviews         on tblbooks.bookid = tblreviews.bookid         left join tblmembers             on tblreviews.userid = tblmembers.userid tblreviews.bookid not null order rating desc limit 0, 40 

i query database , return average rating, if book has been reviewed 6 times, total each rating 6 users , calculate average. right latest rating being returned. when change line to:

round(round(avg(tblreviews.rating) * 2) / 2, 1) rating 

only 1 result returned in total there wrong have no idea what.

if can shed light on this, think have joins.

you need use group by this:

select tblbooks.bookid, tblbooks.title, tblbooks.author, tblbooks.coverimage,        avg(tblreviews.rating) avgrating tblbooks      left join tblreviews      on tblbooks.bookid = tblreviews.bookid tblreviews.bookid not null group tblbooks.bookid, tblbooks.title, tblbooks.author, tblbooks.coverimage order avgrating desc 

your query doesn't seem use information members table, eliminated it. not filtering based on it, because using left join.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -