Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:06:46+00:00 2026-06-13T17:06:46+00:00

I am using MYSQL with PHP to do the server side scripting. I am

  • 0

I am using MYSQL with PHP to do the server side scripting. I am having trouble trying to build a query that aggregates some review data that needs to be joined with some information about who the review is for.

I have two tables. One called “schools” with general information about a school and another called “reviews” with the user supplied review information.

The school table is structured as:

id, schoolName, schoolCity, schoolCountry (for the field names)

With example return data as:

1, ASD, Dubai, UAE
2, DAA, Dubai, UAE

etc.

The “reviews” table has 57 columns:

RID, schoolID, profileID, dateCreated, Q1, Q2, Q3,…Q51, Q52, freeReview (as field names)

with data returned like:

1, 8, 55, 2012-10-10 05:16:05, 10, 8, 9, 2, 3….

From Q1 to Q25, these are questions within a review form that are returned as integers between 0-10. The other columns tie to a profile table and the school table (as above).

I am able to create a query for an individual school that SUMS all of the reviews for the given school and generates an average score for the school based on the number of reviews submitted for that school.

What I am having trouble with is I would like to create a page that lists all (broken up by pagination) the schools in a table with that averaged and SUM-ed score.

This is the query I have that generates the overall average score for a school.

$singlereviewtotal = mysql_query("
SELECT ROUND(
((SUM(Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+
Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+
Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+
Q46+Q47+Q48+Q49+Q50+Q51+Q52)/((52*10)*
(SELECT COUNT(*) FROM reviews WHERE schoolID='$schoolvalue')))*10), 2)
AS total
FROM reviews
WHERE schoolID='$schoolvalue'") or die(mysql_error()); 

This generates the correct summed and averaged value of all of the columns and rows, which is rounded to 2 decimals. The WHERE clause comes from a PHP value in the page that tells the query which school ID to use.

This is my attempt to try to get all of the schools into a list.

$sumreviews = mysql_query("SELECT *, 
 ROUND(
 ((Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+
 Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+
 Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+
 Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)
 *10),2) AS atotal 
 FROM reviews
 RIGHT JOIN schools ON schools.SID = reviews.schoolID
 GROUP BY schools.schoolName, reviews.schoolID
 ORDER BY atotal DESC")or die(mysql_error()); 

This query gives me the list of schools, yet the output for “atotal” only gives me the SUM of the first row for that given school (which in this case is 7.31 – and can be seen just below in the output, but the averaged score should be 7.23).

If I remove the GROUP BY in the query above I get the list of all of the schools, plus extra rows for some schools who have more reviews – like American School of Dubai.

School Name, City, Country, Rating
American School of Dubai, Dubai, U.A.E. 7.31
Dubai American Academy, Dubai, U.A.E. 7.19
American School of Dubai, Dubai, U.A.E. 7.15
International School of Bangkok, Bankok, Thailand
American School of Rio de Janeiro, Rio, Brazil

American School of Budapest, Budapest, Hungary

Zurich International School, Zurich, Switzerland

I am wondering, how to construct the query so that the “AS atotal” rows for all the given schools are grouped and averaged? If I averaged the 7.31 with the 7.15 listed as individual review scores for the American School of Dubai, I would have the correct 7.23. The answer is just right there, but I can’t seem to connect it together.

I apologize in advance for my lack of question “styling”!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:06:47+00:00Added an answer on June 13, 2026 at 5:06 pm

    Have you tried something like

     SELECT schools.schoolName,
     ROUND(AVG(
     ((Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+
     Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+
     Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+
     Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)
     *10)),2) AS average
     FROM reviews
     RIGHT JOIN schools ON (schools.SID = reviews.schoolID)
     GROUP BY schools.SID
     ORDER BY average DESC;
    

    Your last query does not feature any aggregate function, so that might be the reason why it didn’t return the expected results.

    Haven’t tested the query, but if you supplied a SQLFiddle or a sample test data I could verify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:
I am trying to connect to a MySQL server using PHP's 'mysql_connect()' function, but
I have a server-side php-script that fetches results from a MySQL table. I am
I'm using PDO in my PHP application. It connects to a MySQL server on
(Using MySQL and PHP) I have a search form that will allow my users
I'm using MySQL with php. Through a php script i'm trying to import a
I am using MySQL and PHP to build a data layer for a flash
I am using php any mysql. I would like to create a timer that
My project setup is like this Server side I have webserver with PHP,MySQL database
I want to build a chat system in nodeJs + MYSQL using php. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.