如何根据列值过滤结果

时间:2019-06-04 06:10:26

标签: mysql

我在查询中设置了此结果。

+-------------+-------------+-----------+----------+------------------+---------------+----------------+
| Branch Name |  Client ID  |   Name    |  Loans   | Savings Deposits | Time Deposits | Share Capital  |
+-------------+-------------+-----------+----------+------------------+---------------+----------------+
| Main Office | 01-029203-1 | Doe, John |     0.00 |             0.00 |          0.00 |           0.00 |
| Main Office | 01-012314-1 | Doe, John | 51200.75 |            61.26 |      15000.00 |           0.00 |
| Main Office | 01-929828-3 | Doe, Jane |  3000.00 |             0.00 |       8000.00 |           0.00 |
| Main Office | 01-992830-4 | Doe, Jane |  5240.44 |            46.00 |       2000.00 |           0.00 |
+-------------+-------------+-----------+----------+------------------+---------------+----------------+

我要过滤掉,如果某个客户名称的客户ID之一(例如:Doe,John的客户ID为01-029203-1)在所有4个帐户(贷款,储蓄存款,时间)上均具有0余额存款和股本),则不包括该客户的所有实例。

就是这样:

+-------------+-------------+-----------+----------+------------------+---------------+----------------+
| Branch Name |  Client ID  |   Name    |  Loans   | Savings Deposits | Time Deposits | Share Capital  |
+-------------+-------------+-----------+----------+------------------+---------------+----------------+
| Main Office | 01-929828-3 | Doe, Jane |  3000.00 |             0.00 |       8000.00 |           0.00 |
| Main Office | 01-992830-4 | Doe, Jane |  5240.44 |            46.00 |       2000.00 |           0.00 |
+-------------+-------------+-----------+----------+------------------+---------------+----------------+

我已经尝试过使用wherehaving子句,但是我不知道怎么做。

1 个答案:

答案 0 :(得分:0)

一个选项,使用四列的总和:

SELECT *
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable WHERE t1.Name = t2.Name AND
                  Loans + `Savings Deposits` + `Time Deposits` + `Share Capital` = 0);

此答案通过搜索每个记录是否可以找到具有相同名称的所有帐户的总和为零的记录而起作用。如果是这样,则名称无效,并且所有此类名称记录都不会出现在结果集中。