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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:01:27+00:00 2026-06-02T12:01:27+00:00

Please i need your help with how to optimize my query. I want to

  • 0

Please i need your help with how to optimize my query. I want to fetch 10 different rows data from the same table.

Here is one of the queries

     $query1 = mysql_query("SELECT m.Course_code AS 'Course', m.score, m.grade
                   FROM maintable AS m  
                   INNER JOIN students AS s ON
                   m.matric_no = s.matric_no
                   INNER JOIN Courses AS c ON
                   m.Course_code = c.Course_code
                   WHERE m.matric_no = '".$matric_no."'
                   AND m.semester_name = 'hamattarn'
                   AND m.level = '100' ") or die (mysql_error());

                  $number_cols1 = mysql_num_fields($query1) ;

this part of the query is changing for all the queries

  AND m.semester_name = 'hamattarn'
                   AND m.level = '100' "

 AND m.semester_name = 'rain'
                   AND m.level = '100' "

 AND m.semester_name = 'hamattarn'
                   AND m.level = '200' "

 AND m.semester_name = 'rain'
                   AND m.level = '200' "

  AND m.semester_name = 'hamattarn'
                   AND m.level = '300' "

  AND m.semester_name = 'rain'
                   AND m.level = '300' "

  AND m.semester_name = 'hamattarn'
                   AND m.level = '400' "

  AND m.semester_name = 'rain'
                   AND m.level = '400' "

 AND m.semester_name = 'hamattarn'
                   AND m.level = '500' "

 AND m.semester_name = 'rain'
                   AND m.level = '500' "

Here’s a picture of what i’ve being able to achieve with the query. After optimization i still want it to look this way.

< href=”http://i.imgur.com/IGEj2.png&#8221;>Sample Picture

thanks for you time and patience.

UPDATE — here is how i was displaying the row tables from each of the 10 queries before the optimization.. Please how can i achieve this table type reult with the new optimized query?

   echo "<table class=\"altrowstable\" id = " bgcolor = gold >\n";
     echo "<tr align=center>\n";
     for ($i=0; $i<$number_cols10; $i++)
     {
               echo "<th>" . mysql_field_name($query10, $i). "</th>";
     }
     echo "</tr>\n";
               while ($row = mysql_fetch_row($query10))
    {
    echo "<tr align=center>\n";
    for ($i=0; $i<$number_cols10; $i++)
    {
    echo "<td>";
    if (!isset($row[$i]))
    {echo "NULL";}
    else
    {
    echo "<b>".$row[$i]."</b>";
    }
    echo "</td>\n";
    }
    echo "</tr>\n";
    }
    echo "</table>"; 
    echo"</span>" ;

thanks

  • 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-02T12:01:28+00:00Added an answer on June 2, 2026 at 12:01 pm

    Why not something like this:

    WHERE m.semester_name IN ('rain', 'hamattarn') 
      AND m.level IN ('100', '200', '300', '400', '500'))
    

    As the results will be mixed, you should get the results in an expectable order, for example:

    ORDER BY m.semester_name, m.level
    

    This will allow you to split the results in PHP, something like this is common:

    $previous_semester = $previous_level = false;
    foreach(mysql_fetch_array($query) as $row) {
        if ($previous_semester == $row['semester_name']) {
            // row semester is different from the previous one
            echo $row['semester_name'];
        }
    
        if ($previous_level == $row['level']) {
            // row level is different from previous one
            echo $row['level']; 
        }
    
        print_r($row);
    
        $previous_level = $row['level'];
        $previous_semester = $row['semester_name'];
    }
    

    You could open an HTML table before the for loop.
    When the semester or level changes, you could close the previous table and open a new one, add a title for the rest of the rows of the same semester/level, etc, etc…
    You could close an HTML table after the for loop.

    It’s a little tricky but eventually it will get you there.

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

Sidebar

Related Questions

I need your help and please give me some advice. From programming pearls I
guys i need your help, please tell me how can i pass 2 different
Please I need your help with my script. I'm trying to insert values into
Please i need your help with how to include INDEXES in my tables, i've
Please help! I need to produce a Crystal Report with multiple data columns, but
I've reached the end of my Linq rope. Need your help! Heres my table
Please, I need your help. I have a Follow button. When the button is
I need your help. I'm parsing wikipedia article and using Wikimedia api. I want
I really need your help. I have a database with latitude and longitude data,
I really need your help since I have a frustrating problem. I'm downloading data

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.