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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:46:53+00:00 2026-06-02T21:46:53+00:00

I have two tables, result and gp . In the result table I have

  • 0

I have two tables, result and gp.

In the result table I have something like this:

|id||student_id   ||course_code||grade||session  ||level||semester|
|1 ||TR/2213234561||MAT111     ||A    ||2009/2010||100  ||first   |
|2 ||TR/2213234561||MAT112     ||B    ||2009/2010||100  ||first   |
|3 ||TR/2213234561||MAT113     ||C    ||2009/2010||100  ||first   |
|4 ||TR/2213234567||MAT111     ||D    ||2009/2010||200  ||first   |
|5 ||TR/2213234567||MAT112     ||C    ||2009/2010||200  ||first   |
|6 ||TR/2213234567||MAT113     ||C    ||2009/2010||200  ||first   |

Then gp table

|id||student_id   ||session  ||level||semester||gp |
|1 ||TR/2213234561||2009/2010||100  ||first   ||4.2|
|2 ||TR/2213234567||2009/2010||100  ||first   ||3.5|
|3 ||TR/2213234561||2010/2011||200  ||first   ||4.2|
|4 ||TR/2213234567||2010/2011||200  ||first   ||3.5|

What I want is like this:

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP |
|TR/2213234561||A     ||B     ||D     ||C     ||4.2|
|TR/2213234567||C     ||D     ||E     ||F     ||3.5|

The course code are not constant – it depends on the course registered by the students

I have done this:

<?php
$rst1 = mysql_query("select distinct course_code from result ", $conn);
echo "<table callspacing='4'>";
echo "<tr>";
echo "<td> Matriculation Number </td>";

$c_code = array();
while ($row = mysql_fetch_array($rst1))
{
    $c_code[] = $row['course_code'];
}

foreach($c_code as $c_code)
{
    echo "<td>" .$c_code. "</td>";
}

$sql ="SELECT result.student_id,
       MAX(CASE WHEN course_code = ' $c_code' THEN grade END)  $c_code,
       gp.CTC 
       FROM result 
       JOIN gp  
       ON gp.student_id = result.student_id
       GROUP
       BY student_id";

echo "<td> GP</td>";                         
$rst = mysql_query("$sql",$conn) or die(mysql_error());

while ($row = mysql_fetch_array($rst))
{
    echo "</tr>";
    echo "<tr>";
    echo "<td>" .$row['student_id']. "</td>";
    echo "<td>"  .$row[$c_code]. "</td>";
}

echo "<td>"  .$row[$c_code]. "</td>";
echo "<td>" .$row['CTC']. "</td>";

echo"</tr>";
echo "</table>";
?>

The first query was to get the course code, since the courses are not constants.

with that code, I got something like this:

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP|
|TR/2213234561|
|TR/2213234567|

But I wanted

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP |
|TR/2213234561||A     ||B     ||D     ||C     ||4.2|
|TR/2213234567||C     ||D     ||E     ||F     ||3.5|

Any suggestion or direction will be highly 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-02T21:46:54+00:00Added an answer on June 2, 2026 at 9:46 pm

    What you wish to do is known as “pivoting” your data and is something for which some other RDBMS have native support, but MySQL does not (by design, as the developers feel that such manipulations belong in the presentation layer).

    However, you have a few options:

    1. Construct a rather horrible MySQL query to perform the pivoting operation manually:

      SELECT student_id AS Matriculation, MAT111, MAT112, gp AS GP
        FROM gp
        NATURAL JOIN (
          SELECT student_id, grade AS MAT111
          FROM result
          WHERE course_code = 'MAT111'
        ) AS tMAT111
        NATURAL JOIN (
          SELECT student_id, grade AS MAT112
          FROM result
          WHERE course_code = 'MAT112'
        ) AS tMAT112
        -- etc.
      WHERE level = @level AND semester = @semester
      

      If you choose to go down this path, you can make your life slightly easier by generating this query automatically, using either a looping construct in PHP or a prepared statement in MySQL.

      Here is one way that you could do that in PHP:

      1. Obtain a list of courses:

        $dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', $user, $password);
        $qry = $dbh->query("SELECT DISTINCT course_code FROM result [WHERE ...]");
        $courses = $qry->fetchAll(PDO::FETCH_COLUMN, 0);
        
      2. Loop over the results, constructing the above SQL:

        mb_regex_encoding($charset);
        
        $columns = mb_ereg_replace('`', '``', $courses);
        $sql = "
        SELECT student_id AS Matriculation, `".implode("`,`", $columns)."`, gp AS GP
          FROM gp";
        
        foreach ($columns as $column) $sql .= "
          NATURAL JOIN (
            SELECT student_id, grade AS `$column`
            FROM result
            WHERE course_code = ?
          ) AS `t$column`";
        
        $sql .= "
        WHERE level = ? AND semester = ?";
        
      3. Execute the SQL, passing in the array of courses as parameters:

        $qry = $dbh->prepare($sql);
        
        $params = $courses;
        array_push($params, $level, $semester);
        $qry->execute($params);
        
      4. Output the results:

        echo "<table>";
        
        echo "<tr>";
        for ($i = 0; $i < $qry->columnCount(); $i++) {
          $meta = $qry->getcolumnMeta($i);
          echo "<th scope='col'>" . htmlentities($meta['name']) . "</th>";
        }
        echo "</tr>";
        
        while ($row = $qry->fetch(PDO::FETCH_NUM)) {
          echo "<tr>";
          foreach ($row as $field) echo "<td>" . htmlentities($field) . "</td>"
          echo "</tr>";
        }
        
        echo "</table>";
        
    2. Do the above as a one-off operation so that the structure of your MySQL database is changed to more closely reflect this desired layout (easy once table is converted, but may impact other uses of the database):

      CREATE TABLE StudentGrades (PRIMARY KEY('Matriculation'))
      SELECT student_id AS Matriculation, MAT111, MAT112, gp AS GP
        -- etc. as above
      

      Alternatively, you can create a VIEW which is a sort of “virtual table” structured in this way based on the underlying table.

    3. Pivot the data manually in PHP (relatively tedious).

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

Sidebar

Related Questions

I have got two tables: tbl_sms tbl_bids The above two table holds something like
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have two tables like this. Table1 Column | Type | ---------+------------------+ cod |
I have two tables: source table result table I have an after update trigger
I have two short question regarding Azure Storage Tables. Is there something like an
I have two tables like this: Table1ID Table2ID Table1ID SomeDate -------- ------------------------------ 1 1
Feel like this should be something easy that I'm missing. I have a table
I have a MySQL table that looks something like this: +--------------------------------------+ | id |
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables which I would like to join by ID field. I

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.