MySQL查询相关问题

时间:2009-10-04 06:31:10

标签: sql mysql

设置:

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)

我的第一个问题:

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; 

返回:

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

我的第二个问题:

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; 

返回:

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

现在我想通过简单地从第一个结果中扣除第二个结果来得到这样的结果:

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

要获得此结果,我应该运行什么mysql查询?

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

在这种情况下,您可以使用CASE表达式:

SELECT
    bank_name,
    sum(CASE WHEN method=0 THEN amount WHEN method=1 THEN -amount ELSE 0 END) AS amount
FROM accounts_bank ab
    LEFT JOIN bank b ON b.bank_id=ab.bank_id
GROUP BY bank_name
ORDER BY bank_name

如果您需要更复杂的处理,您可以简单地将两个查询组合在一起:

SELECT bank_name, sum(amount) AS amount
FROM
  (SELECT bank_id, sum(amount) as amount
   FROM accounts_bank WHERE method=1
   UNION
   SELECT bank_id, -sum(amount) as amount
   FROM accounts_bank WHERE method=0) ab
  LEFT JOIN bank b ON b.bank_id=ab.bank_id
GROUP BY bank_name
ORDER BY bank_name

答案 2 :(得分:0)

我注意到你为所有银行设置'bank_id'= 1。它们不应该是不同的数字吗?这可能是您问题的一部分。