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 6739953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:32:55+00:00 2026-05-26T11:32:55+00:00

Below is my Query: $query = SELECT * FROM Teacher t INNER JOIN Session

  • 0

Below is my Query:

    $query = "
      SELECT *
      FROM Teacher t
      INNER JOIN Session s ON t.TeacherId = s.TeacherId
      JOIN Grade_Report gr ON s.SessionId = gr.SessionId
      WHERE
        ('".mysql_real_escape_string($sessionid)."' = '' OR gr.SessionId = '".mysql_real_escape_string($sessionid)."') ";

The results are stored in a table which code looks like this:

<table border='1'>
      <tr>
      <th>Session ID</th>
      <th>Student Username</th>
      <th>Student Name</th>
      <th>Mark</th>
      <th>Grade</th>
      </tr>
      <?php
        while ($row = mysql_fetch_array($result)) {
          echo "
      <tr>
      <td>{$row['SessionId']}</td>
      <td>{$row['StudentUsername']}</td>
      <td>{$row['StudentForename']} {$row['StudentSurname']}</td>
      <td>{$row['Mark']}</td>
      <td>{$row['Grade']}</td>
      </tr>";
        }

This query outputs 13 rows in the results which is fine when it selects all rows.

Now what happens is that I want the average mark for each session. So I include 'AVG(gr.Mark) as AvgMark' in the query and include <td>{$row['AvgMark']}</td> in the table. The Calculation it outputs is correct The problem is that the query now only outputs 1 row which is the top row of the query result. I understand this as AVG(gr.Mark) only requires one row when outputted.

So my question is that except using SELECT‘AVG(gr.Mark) as AvgMark’ in the query to find average marks of each session, is there a way I can workout average of each mark outside the query by using php so that the query output shows the 13 rows and is not affected and I can store the calculation of the average below the table rather than in the table?

  • 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-05-26T11:32:56+00:00Added an answer on May 26, 2026 at 11:32 am

    The reason AVG returns one row is that it’s an aggregate function, and without a GROUP BY clause operates on all rows.

    Averages in PHP are just like averages everywhere else; total your values and divide by the number of samples. Hence, you can easily calculate the average in code by changing the code to:

          <?php
            $total = 0;
            $count = 0;
            while ($row = mysql_fetch_array($result)) {
              $count++;
              $total += $row['Mark'];
              echo "
          <tr>
          <td>{$row['SessionId']}</td>
          <td>{$row['StudentUsername']}</td>
          <td>{$row['StudentForename']} {$row['StudentSurname']}</td>
          <td>{$row['Mark']}</td>
          <td>{$row['Grade']}</td>
          </tr>";
            }
    $average = (int)($total/$count);
    echo "<tr><td colspan=3></td><td>Average</td><td>$average</td></tr>".
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the below query explain? SELECT * FROM `jos_menu` WHERE (id = 69
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have a very large query that follows the format below: select ... from
I would like to select multiple table in one query say like below and
Below is my query: $query = SELECT gr.SessionId, t.TeacherUsername, s.ModuleId, m.ModuleName, st.CourseId, st.Year, st.StudentUsername,
Consider the SQL query below: SELECT * FROM opening_hours WHERE week_day = WEEKDAY(NOW()) +
We'd like to write this query: select * from table where col1 != 'blah'
Here is the below Code: $query = mysql_query(SELECT * FROM tablex); if ($result =
Below is my query select @monNameStr as [MName], IsNull(count(c.AssignmentID),0), IsNull(sum(s.ACV),0), IsNull(sum(s.GrossReturn),0), IsNull(sum(s.NetReturn),0), IsNull(avg(a.Total),0) FROM
How can I execute the results of my select query. The query below gives

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.