使用 max returns null 的 PostgreSQL 交叉连接
PostgreSQL cross join using max returns null
我想 select 几个表中每个表中 modified_at 列的 max() 值:
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a, b, c;
只要每个表至少有 1 行,这就可以正常工作。
问题是当只有一个表有 0 行时,所有表都返回 null:
null,null,null
什么是 returns 具有行的表的正确值的解决方案?
PostgreSQL-10
使用 OUTER JOIN
s 应该可以做到
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a outer join b outer join c;
但一个可能更简单的选择是改用 3 个子查询:
select
(select max(modified_at) from a) as a_modified_at,
(select max(modified_at) from b) as b_modified_at,
(select max(modified_at) from c) as c_modified_at;
我想 select 几个表中每个表中 modified_at 列的 max() 值:
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a, b, c;
只要每个表至少有 1 行,这就可以正常工作。 问题是当只有一个表有 0 行时,所有表都返回 null:
null,null,null
什么是 returns 具有行的表的正确值的解决方案? PostgreSQL-10
使用 OUTER JOIN
s 应该可以做到
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a outer join b outer join c;
但一个可能更简单的选择是改用 3 个子查询:
select
(select max(modified_at) from a) as a_modified_at,
(select max(modified_at) from b) as b_modified_at,
(select max(modified_at) from c) as c_modified_at;