【发布时间】:2013-03-24 03:37:05
【问题描述】:
假设我有以下 MySQL 视图:
create or replace view total_transactions(account_id, total) as
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
group by t.bank_account_id;
假设帐户还没有任何交易,我希望视图返回 0。 现在,如果我选择如下:
select * from total_transactions where account_id = 2060;
账户 2060 没有任何交易,它不会返回任何东西,而不是 0。
我该如何解决这个问题?
提前致谢。
编辑
我认为这可能与group by...有关。
如果我在没有 group by 的情况下执行我用于视图的查询,它可以工作(即使没有结果也返回 0),但如果我使用 group by,它会返回 null:
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
and account_id = 2060;
返回0,然后
create or replace view total_transactions(account_id, total) as
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
and account_id = 2060
group by t.bank_account_id;
返回一个空集。
【问题讨论】: