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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:35:19+00:00 2026-06-05T01:35:19+00:00

Sorry for the real simple question, I just learn PHP & MySQL, I already

  • 0

Sorry for the real simple question, I just learn PHP & MySQL, I already googling it for more than a week but I didn’t found any answer.

I create a simple finance script and the table is like below :

table_a
aid | value
1   | 100
2   | 50
3   | 150

table_b
bid | aid | value
1   | 1   | 10
2   | 1   | 15
3   | 2   | 5
4   | 2   | 10
5   | 3   | 25
6   | 3   | 40

I want the result like this

No | ID | Total | Balance
1  | 1  | 10    | 90
2  | 1  | 25    | 75
3  | 2  | 5     | 45
4  | 2  | 15    | 35
5  | 3  | 25    | 125
6  | 3  | 65    | 85

Can anybody help me with my problem?

Thanks

  • 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-05T01:35:21+00:00Added an answer on June 5, 2026 at 1:35 am
    SELECT
       tb.bid as No,
       ta.aid as ID,
       tb.value as Total,
       ta.value-tb.total as Balance
    FROM
      table_a AS ta
      INNER JOIN (
        SELECT
          tbx.aid AS aid,
          tbx.bid AS bid,
          tbx.value AS value,
          SUM(tby.value) AS total
        FROM 
          table_b AS tbx
          INNER JOIN table_b AS tby ON tby.aid=tbx.aid AND tby.bid<=tbx.bid
        GROUP BY tbx.bid
        ORDER BY tbx.bid
      ) AS tb ON tb.aid=ta.aid
    ORDER BY tb.bid
    

    As @Quassnoi pointed out, this is not very efficient with MySQL. I tried to use a freak join instead of a subquery, as the inner query might be of use in its own right.

    Edit

    Took some interest in this and found the join version to be twice as fast as the subquery version by @Quassnoi … anybody having an idea why this would be?

    Edit

    Answer to the second question (in comment below):

    SELECT
      table_a.aid AS aid,
      SUM(table_b.value) AS Total,
      table_a.value-SUM(table_b.value) AS Balance
    FROM
      table_a
      INNER JOIN table_b ON table_a.aid=table_b.aid
    GROUP BY table_a.aid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sorry if this is more of a theory question then a real
Should be a simple question, I'm just not familiar with PHP syntax and I
(Sorry if this sounds like a rant, but it's a real question and I'd
i am new in php/mysql and sorry if my question is silly. I am
Sorry for this not being a real question, but Sometime back i remember seeing
[Updated, sorry about the change but now to the real problem] I cannot include
Sorry for a long question and not a very descriptive title, but my problem
Sorry, I'm sure this is simple but I'm tired and can't figure it out.
sorry for the lame question but with HTTP handler it is easy to do
Sorry, this is probably a duplicate question, but how can I iterate over a

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.