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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:44:31+00:00 2026-06-04T23:44:31+00:00

I’m sure this sort of question has come up before but I’ve been searching

  • 0

I’m sure this sort of question has come up before but I’ve been searching and can’t find anything similar to what I need.

Edit: so after some reading this looks like it falls under pivots and uses group concat. If anybody has any insight id really appreciate it.

I have 3 tables (unnecessary fields & data stripped out for simplicity):

Students
id       name
------------------
1        John
2        Jane



Tests
id     name
------------------
1      Test1
2      Test2



Results
id    test_id     student_id   result
--------------------------------------
1     1           1            90
2     1           2            70
3     2           1            50
4     2           2            95

What I want is to be able to produce a table like this:

Name     Average    Test1    Test2
-----------------------------------
John     70         90        50
Jane     92.5       70        95

I know how to get the average, and I’m sure I could do this with an ugly set of loops and php logic but I’d like to get the most efficient solution. Any help is greatly appreciated.

  • 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-04T23:44:32+00:00Added an answer on June 4, 2026 at 11:44 pm
    SELECT
        s.name,
        avg(r.result) AS average,
        t1.result AS test1,
        t2.result AS test2
    FROM
        students s,
        results r,
        results t1,
        results t2
    WHERE
        r.student_id = s.id AND
        t1.test_id = 1 AND
        t1.student_id = s.id AND
        t2.test_id = 2 AND
        t2.student_id = s.id
    GROUP BY s.id;
    
    +------+---------+-------+-------+
    | name | average | test1 | test2 |
    +------+---------+-------+-------+
    | John |      70 |    90 |    50 | 
    | Jane |    82.5 |    70 |    95 | 
    +------+---------+-------+-------+
    

    Edit: You can’t really make the test columns dynamically based on the contents of the tests table. You can pick specific values to use as columns though.

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.