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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:22:37+00:00 2026-06-12T01:22:37+00:00

I have a MySQL table with about 100 columns. All columns are INT. I

  • 0

I have a MySQL table with about 100 columns. All columns are INT. I want to be able to select a row by its ID, and get the total number of all of the columns added together.

There are two additional considerations. The field titles vary significantly, so I would like to be able to have it work by referencing the field number, rather than the field title.

Lastly, I would like to potentially be able to specify a range, for example, add total of field 35 through field 76 where id = 50.

Is there a way to do this? Thank you very much!

  • 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-12T01:22:38+00:00Added an answer on June 12, 2026 at 1:22 am

    You need to build dynamically a query.

    First thing you need the column names (you can retrieve them automatically too, but need an extra query). Best thing is to put them in a array:

    $Fields = array(
       'id', 'fld2', 'fld3', ..., 'fld99'
    );
    
    function getSumField($from, $to, $allow_null = False)
    {
        GLOBAL $Fields;
        $a_sum = array();
        for($i = $from; $i <= $to; $i++)
        {
            if ($allow_null)
                $a_sum[] = "COALESCE({$Fields[$i]},0)";
            else
                $a_sum[] = $Fields[$i];
        }
        return '('.implode('+', $a_sum).')';
    }
    
    // So we want: sum of fields 17-42 of the ONE ROW where ID = 5
    
    $fld = getSumField(17, 42);
    $SQL = "SELECT $fld FROM tbl WHERE id = 5;";
    
    // Sum of same, in rows with ids from 7 to 22
    $SQL = "SELECT SUM($fld) AS total FROM tbl WHERE id BETWEEN 7 AND 22;";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with about 5,000,000 rows that are being constantly updated
I have a mysql table with about 2,000,000 entries, with a primary key which
I have a mysql DB table of about 10.000 rows and about 20 fields.
Greetings friends, In my MySQL database, I have 'MAB' table which contains information about
I have few thousands of records with few 100 fields in a MySQL Table.
I have this table similar to MySQL - get sum() grouped max() of group
Hi I have two questions about MySQL and QUERIES. First of all i've like
I have a large mysql table (about 5M rows) on which i frequently insert
I have a MySQL table , each row representing a Person. For each person
I have a MySQL table with about 2 million rows, and a script that

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.