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

  • Home
  • SEARCH
  • 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 971287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:24+00:00 2026-05-16T02:56:24+00:00

I have a MYSQL table that is 1275 fields wide. Each row in the

  • 0

I have a MYSQL table that is 1275 fields wide. Each row in the table represents a single class of students, with 17 fields per student, X up to 75 students per class, so 17 X 75 = 1275 fields.

I have devised an SQL UNION query that successfully pulls the students into another table, with each student on a single row.

Now, I want to use this UNION query as part of a PHP program. I have “imported” the query into PHP as it is. But, isn’t there a way to use PHP to shorten the SQL query? Shamelessly, here is my code:

    $sql = "
    INSERT INTO $t_mem2
    SELECT localcourse
         , statecourse
         , coursetitle
         , semester
         , section
         , teachercode
         , teachername
         , meetingcode
         , classpop
         , student_id_01 AS student_id
         ,        sex_01 AS sex
         ,        dob_01 AS dob
         ,      grade_01 AS grade
         ,     ethnic_01 AS ethnic
         ,  last_name_01 AS last_name
         , first_name_01 AS first_name
         , $c_sch        AS sch_code
    FROM $t_mem1
    UNION
    SELECT localcourse
         , statecourse
         , coursetitle
         , semester
         , section
         , teachercode
         , teachername
         , meetingcode
         , classpop
         , student_id_02 AS student_id
         ,        sex_02 AS sex
         ,        dob_02 AS dob
         ,      grade_02 AS grade
         ,     ethnic_02 AS ethnic
         ,  last_name_02 AS last_name
         , first_name_02 AS first_name
         , $c_sch        AS sch_code
    FROM $t_mem1
    UNION 
    SELECT localcourse
         , statecourse
         , coursetitle
<...snip..............................>
     , teachername
     , meetingcode
     , classpop
     , student_id_75 AS student_id
     ,        sex_75 AS sex
     ,        dob_75 AS dob
     ,      grade_75 AS grade
     ,     ethnic_75 AS ethnic
     ,  last_name_75 AS last_name
     , first_name_75 AS first_name
     , $c_sch        AS sch_code
  FROM $t_mem1
 ORDER
    BY localcourse
     , statecourse
     , semester
     , section
     , teachername
     , meetingcode
     , last_name
     , first_name" ;
  • 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-16T02:56:24+00:00Added an answer on May 16, 2026 at 2:56 am

    First off, this query indicates that your database schema is very, very poor.

    That aside, yes, you can shorten the query with PHP:

    $query = "INSERT INTO $t_mem2 ";
    for ($i = 1; $i <= 75; $i++) {
        if ($i > 1) {
            $query .= ' UNION ';
        }
        $s = "$i";
        if ($i < 10) {
           $s = '0'.$s;
        }
        $query .= "SELECT localcourse
             , statecourse
             , coursetitle
             , semester
             , section
             , teachercode
             , teachername
             , meetingcode
             , classpop
             , student_id_{$s} AS student_id
             ,        sex_{$s} AS sex
             ,        dob_{$s} AS dob
             ,      grade_{$s} AS grade
             ,     ethnic_{$s} AS ethnic
             ,  last_name_{$s} AS last_name
             , first_name_{$s} AS first_name
             , $c_sch        AS sch_code FROM $t_mem1";
    }
    $query .= " ORDER
    BY localcourse
     , statecourse
     , semester
     , section
     , teachername
     , meetingcode
     , last_name
     , first_name" ;
    

    This will “shorten the query” by generating it in PHP code. If, on the other hand, you’re looking to make the query as it hits the database shorter, that is also possible; just modify the loop above to start $query anew in each iteration, and glom all the results together in one array. You’ll end up with 75 queries made, and PHP will perform the UNION.

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

Sidebar

Related Questions

No related questions found

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.