Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-17-2002, 10:12 AM   PM User | #1
koenig
New to the CF scene

 
Join Date: Oct 2002
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
koenig is an unknown quantity at this point
Question MYSQL JOIN and AVG problem

Hello!!

I have a prob with Mysql.

I got two tables

First : material with fields 'id' and 'name'
Second: ratings with fields 'material_id', 'comment' and 'rating'

Now I want to select all material with the average rating associated.

So I did
"SELECT m.id,AVG(b.rating) FROM material as m LEFT JOIN bewertungen as b ON m.id = b.materialid AND b.rating > 0 GROUP BY id" .

It works so far as it returns all material_ids associated with the average rating for this material or NULL if no rating is present in the ratings table.

Now I want to sort the results by AVG(rating) but i cannot add "ORDER BY AVG(rating)" as this results in an error.

Can anyone help me, please?

Greetings
Fabian Koenig
koenig is offline   Reply With Quote
Old 10-17-2002, 10:56 PM   PM User | #2
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Try to use an alias for "AVG(b.rating)", like "AVG(b.rating) AS averages" and sort on that by using "ORDER BY averages".
mordred is offline   Reply With Quote
Old 10-18-2002, 11:29 AM   PM User | #3
koenig
New to the CF scene

 
Join Date: Oct 2002
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
koenig is an unknown quantity at this point
thanks very much... it works great
koenig is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:54 PM.


Advertisement
Log in to turn off these ads.