Forum Moderators: open
I'm not really a newbie but I've never came accross such a concept and I am a little lost I have to say.
I have two tables:
Schools
- id
- name
- address1
- phone
- active
Ratings
- id
- idschool
- rating
- user
And I need to display all of the schools ordering them by their rating average?
I know this will involve join etc but each time I take a step I can't seem to model the whole process in my mind, I would greatly appreciate any help! :)
Thanks
Andie
This is exactly what I was looking for... just another question though, right now if one of the schools has no reviews the query doesn't select/display it...
Is there any way to make it so that schools that have no reviews will still show up as if they had a zero rating score?
Thanks again!
Andie
$query="SELECT name, idcategory, address1, address2, city, state, zip, phone, maps, AVG(rating) AS avg_rating FROM schools LEFT JOIN reviews ON schools.id = reviews.idschool WHERE schools.idcategory = '$category' GROUP BY schools.id ORDER BY avg_rating DESC";
$result=mysql_query($query) or die ("Query failed");
$row = mysql_fetch_array($result);
$idschool = $row["id"];
Thanks :)
Andie
$query="SELECT name, idcategory, address1, address2, city, state, zip, phone, maps, AVG(rating) AS avg_rating, schools.id AS sch FROM schools LEFT JOIN reviews ON schools.id = reviews.idschool WHERE schools.idcategory = '$category' GROUP BY sch ORDER BY zip";
$result=mysql_query($query) or die ("Query failed");
$row = mysql_fetch_array($result);
$idschool = $row["sch"];
Honestly I am at a loss why using schools.id didn't work and am curious to know if anyone could cast a light there. But I am glad to have this solved anyway.
Thanks again,
Andie
I normally append "ID" to the table name as a field name when I use ID fields (e.g., "SchoolsID" and "RatingsID" or "Schools_ID" and "Ratings_ID" in your case).