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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:41:38+00:00 2026-06-02T07:41:38+00:00

I have been having the most difficult time ever with this problem. I have

  • 0

I have been having the most difficult time ever with this problem. I have 2 tables with total columns that I want to SUM together. They both have the same columns, I am using two tables as one is a script generated table of data and the other is user entered data and we need them separate. Except now we need to SUM(total) them together.

Table 1

   +-----------+-----+--------+------+
   | date      |t_id | t_port | total|
   +-----------+-----+--------+------+
   |2012-04-01 | 1271| 101    | 80.00|
   +-----------+-----+--------+------+

Table 2

+----------+------+--------+-------+
| date     | t_id | t_port | total |
+----------+------+--------+-------+
|2012-04-20| 1271 | 101    | 120.00|
+----------+------+--------+-------+

Total should be $200.00

HERE IS MY QUERY

"SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"

This query seems to work in phpMyAdmin as I get 2 rows. (1 for each table), logically I used a WHILE loop in PHP to add the two rows together. After echo’ing out each row manually I discovered my second row isn’t showing up in the loop, yet it does in the query?

Can’t figure out why this is happening, I am certain it’s something silly but I been at this code for over 16hrs already and need a new set of eyes.

PHP CODE

function periodTotal()
{
    include('/sql.login.php');  

    $t_id                   = "1271";
    $t_port                 = "101";
    $date                   = date("Y-m-d");

    # FIND MONTH (DATE)
    $monthStart     = date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 
    $monthFirst     = date("Y-m-d", strtotime('-1 second',strtotime('+15 days',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
    $monthSecond    = date("Y-m-d", strtotime('-1 second',strtotime('+16 days',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
    $monthEnd       = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));

    if ($date = $monthFirst) 
    {
        $sql = $dbh->prepare("SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthStart' AND '$monthFirst') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthStart' AND '$monthFirst') AND t_port = '$t_port' AND t_id = '$t_id'");
        $sql->execute();
    } 
    else 
    {
        $sql = $dbh->prepare("SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'");
        $sql->execute();
    }
        while($row = $sql->fetch(PDO::FETCH_ASSOC))
        {
            $total += $row['total'];
        }

    return $total;
}
  • 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-02T07:41:40+00:00Added an answer on June 2, 2026 at 7:41 am

    Does this work for you?

    SELECT SUM(`total`) as `total` FROM ((
        SELECT SUM(cntTotal) as total FROM CBS_WO
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'
    ) UNION (
        SELECT SUM(cntTotal) as total FROM CNT_MODS
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"
    )) as temp
    

    This might be more efficient:

    SELECT SUM(total) FROM (
        SELECT cntTotal FROM CBS_WO
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'
    ) UNION (
        SELECT cntTotal FROM CNT_MODS
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"
    ) as temp
    

    (only has one SUM) but you’d have to test it.

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

Sidebar

Related Questions

I have been having an issue with the $this->delete() method that deletes a record
I have been struggling with this for quite some time having been accustomed to
I have been having this problem for a couple of weeks now. The problem
I have been having the following problem, i think it's probably due to the
We have been having some debate this week at my company as to how
I have been having this issue in iterating through an array of keys and
Hello I have been having trouble with this for a while now. I have
Good day everyone. I have been having the same problem all day at work
I tried endlessly now you resolve this utter frustrating situation I have been having.
I have been creating Unit tests like crazy and find that I'm often having

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.