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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:20:25+00:00 2026-05-28T15:20:25+00:00

This is a follow up from my question: MySQL: Querying a table and possibly

  • 0

This is a follow up from my question: “MySQL: Querying a table and possibly replacing some fields with values from another table”

I have two tables. Table1 is our main table of data and Table2 has a subset of the Table1 columns and a subset of the records contained in Table1. You can see Table2 as a table holding corrected values for some of Table1’s records.

Here are my tables with sample data:

Table1

| page_id | field_id | date       | value | bla   |
--------------------------------------------------|
|     1   |      1   | 2012-01-01 |   3   |   1   |
|     2   |      1   | 2012-01-01 |   10  |   2   |
|     3   |      1   | 2012-01-01 |   25  |   2   |
|     1   |      2   | 2012-01-01 |   11  |   3   |
|     2   |      2   | 2012-01-01 |   22  |   1   |
|     3   |      2   | 2012-01-01 |   33  |   2   |

Table2

| page_id | field_id | date       | value | 
-------------------------------------------
|     1   |      1   | 2012-01-01 |   1   |
|     2   |      1   | 2012-01-01 |   2   |
|     3   |      1   | 2012-01-01 |   3   |
|     4   |      1   | 2012-01-01 |   4   |

I want to calculate the sum of values for one or more field_ids, following these rules:

(1) If records for one of the specified field_id exists only in Table1, I want to sum both value and bla for all records where field_id match the specified field_id

(2) If the field_id exists in both Table1 and Table2, then we would get the sum of value from Table2 and the sum of bla from Table1.

Here is the query I am using:

SELECT t1.field_id,
SUM(IFNULL(t2.value, IFNULL(t1.value,0))) as 'Value Sum',
SUM(IFNULL(t1.bla,0)) as 'Bla Sum'
FROM test.Table1 t1
LEFT JOIN test.Table2 t2 ON t1.field_id = t2.field_id
WHERE (t1.field_id IN (1,2))
GROUP BY t1.field_id, t1.date;

But I am not getting the correct sums for field_1:

|field_id | Value Sum  | Bla Sum| 
---------------------------------
|      1  |      30    |   20   | <-- was expecting 10 and 5, respectively 
|      2  |      66    |    6   |

Would you have any idea what I could be doing wrong?

Thanks in advance!

  • 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-28T15:20:26+00:00Added an answer on May 28, 2026 at 3:20 pm

    You need to add also the page_id to the JOIN otherwise the 3 row from table1 and 4 of table2 for field_id1 result in 12 columns

    SELECT t1.field_id,
    SUM(IFNULL(t2.value, IFNULL(t1.value,0))) as 'Value Sum',
    SUM(IFNULL(t1.bla,0)) as 'Bla Sum'
    FROM test.Table1 t1
    LEFT JOIN test.Table2 t2 ON t1.field_id = t2.field_id AND t1.page_id =t2.page_id
    WHERE t1.field_id IN (1,2)
    GROUP BY t1.field_id;
    

    also removed the group by t1.date

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

Sidebar

Related Questions

This is a follow-up to my question from yesterday . I have Scott Meyers'
PHP mysql database I have created a follow on question to this one here
To follow on from my question yesterday.... MySQL Table Design for a Questionnaire I
This is a follow up of another question here on SO . I have
This is a follow up from a question of mine that was just answered
This is a follow-on from this question, in which I was trying to suppress
This is a follow-on question from the one I asked here . Can constraints
This is a follow on from my previous question although this is about something
This question is a follow-up from How to indicate that a method was unsuccessful
This question is a follow on from this one ... I am binding to

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.