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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:27:36+00:00 2026-05-30T03:27:36+00:00

I have two tables containing three columns containing the same types of information(id, attributes,

  • 0

I have two tables containing three columns containing the same types of information(id, attributes, and amounts). I would like to combine them in a union and join that union with another table containing a list of id’s to filter them, then sum common attributes.

table_a                     table_b                   table_ids
id   attr    amount         id   attr     amount      uid     a_id
1    'atr1'  10             1    'atr2'   4           'id1'   1
1    'atr2'  5              2    'atr3'   2           'id2'   2
2    'atr3'  7              2    'atr1'   11
3    'atr1'  8

I tried

SELECT table_ids.uid, t.attr, SUM(t.amount)
FROM table_ids
JOIN (SELECT CONCAT('a', id) AS comb_id, attr, amount FROM table_a
    UNION ALL
    SELECT CONCAT('b', id) AS comb_id, attr, amount FROM table_b
    ) t
ON CONCAT('a', table_ids.a_id) = t.comb_id
GROUP BY t.attr

but am only getting the attribute for last matching a_id and the number of matching rows times the last matching row amount for the sum.

(There will be another table with more id’s filtering table_b attributes but I haven’t even gotten one filtering table to work yet.)

  • 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-30T03:27:38+00:00Added an answer on May 30, 2026 at 3:27 am

    Hard to be 100% sure what you are aiming for but something like this may fit the bill

    SELECT
        joinedData.uid,
        joinedData.attr,
        SUM(joinedData.amount) AS amount
    FROM (
        (
            SELECT
                table_ids.uid,
                table_a.attr,
                SUM(table_a.amount) AS amount
            FROM table_a
            INNER JOIN table_ids
            ON table_ids.a_id = table_a.id
            GROUP BY table_ids.uid, table_a.attr
        )
        UNION
        (
            SELECT
                table_ids.uid,
                table_b.attr,
                SUM(table_b.amount) AS amount
            FROM table_b
            INNER JOIN table_ids
            ON table_ids.b_id = table_b.id
            GROUP BY table_ids.uid, table_b.attr
        )
    ) AS joinedData
    GROUP BY joinedData.uid, joinedData.attr
    

    Have of course assumed that table_b will join id onto table_ids.b_id, change it as required.

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

Sidebar

Related Questions

I have multiple MySQL tables containing varying numbers of columns. After joining three of
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two mysql tables, one containing details on cars and one containing all
I have a table containing two columns in SQL that I want to extract
I have an issue with a table containing three cells in one row. Two
I have a table containing two columns allowable and thickness. The thickness values are
I have two tables: Personnel with ID,Name,... columns and User with PersonnelID (FK of
I have two tables. Table #1 'color' has columns [red], [blue], [green]; where each
I have two tables containing two different kinds of comments. One of the tables
I have two tables that are joined together. A has many B Normally you

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.