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

  • Home
  • SEARCH
  • 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 385849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:31:11+00:00 2026-05-12T15:31:11+00:00

setup: mysql> create table bank(bank_id integer, bank_name varchar(255)); Query OK, 0 rows affected (0.27

  • 0

setup:

mysql> create table bank(bank_id integer, bank_name varchar(255));
Query OK, 0 rows affected (0.27 sec)

mysql> create table accounts_bank(method tinyint, bank_id integer, amount float);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into bank(bank_id, bank_name) values(1, 'A Bank');
Query OK, 1 row affected (0.05 sec)

mysql> insert into bank(bank_id, bank_name) values(2, 'B Bank');
Query OK, 1 row affected (0.03 sec)

mysql> insert into bank(bank_id, bank_name) values(3, 'C Bank');
Query OK, 1 row affected (0.03 sec)

mysql> insert into bank(bank_id, bank_name) values(4, 'D Bank');
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 1, 2500);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (1, 2, 2500);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (1, 3, 5000);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 4, 500);
Query OK, 1 row affected (0.05 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 4, 5800);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (1, 3, 25000);
Query OK, 1 row affected (0.02 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 2, 27500);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 1, 2000);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 1, 2500);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (1, 2, 2500);
Query OK, 1 row affected (0.03 sec)

mysql> inseert into accounts_bank(method, bank_id, amount) values (0, 4, 2500);
Query OK, 1 row affected (0.03 sec)

my first query:

mysql> select bank_name, sum(amount) as amount from accounts_bank ab left join bank b on b.bank_id=ab.bank_id where method=0 group by bank_name order by bank_name; 

returns:

+-----------+--------+ 
| bank_name | amount | 
+-----------+--------+ 
| A Bank    |   7000 | 
| B Bank    |  27500 | 
| D Bank    |   8800 |
+-----------+--------+ 
3 rows in set (0.00 sec) 

my second query:

mysql> select bank_name, sum(amount) as amount from accounts_bank ab left join bank b on b.bank_id=ab.bank_id where method=1 group by bank_name order by bank_name; 

returns:

+-----------+--------+ 
| bank_name | amount | 
+-----------+--------+ 
| B Bank    |   5000 | 
| C Bank    |  30000 |
+-----------+--------+ 
2 rows in set (0.00 sec) 

Now I want a result like this by simply deducting 2nd result from the 1st one:

+-----------+--------+ 
| bank_name | amount | 
+-----------+--------+ 
| A Bank    |   7000 | 
| B Bank    |  22500 |
| C Bank    | -30000 | 
| D Bank    |   8800 |
+-----------+--------+ 

To get this result what mysql query should I run?

  • 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-12T15:31:12+00:00Added an answer on May 12, 2026 at 3:31 pm
    select subquery.bank_name, sum(subquery.amount) from
        (
        select bank_name, sum(amount) as amount from accounts_bank ab left join bank b on
            b.bank_id=ab.bank_id where method=0 group by bank_name
        union all
        select bank_name, sum(-amount) as amount from accounts_bank ab left join bank b on
            b.bank_id=ab.bank_id where method=1 group by bank_name
        ) as subquery group by subquery.bank_name order by subquery.bank_name
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For PHP 5.3 this should work. You may need to… May 13, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer it would be easier w/ out the left joins. as… May 13, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer I put that call into app/config/config.php May 13, 2026 at 4:47 pm

Related Questions

setup: mysql> create table main(id integer unsigned); mysql> create table test1(id integer unsigned,body text);
setup: mysql> create table product_stock( product_id integer, qty integer, branch_id integer); Query OK, 0
setup: mysql> create table product_stock( product_id integer, qty integer); Query OK, 0 rows affected
We are using MySql 5.0 on Ubuntu 9.04. The full version is: 5.0.75-0ubuntu10 I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.