我在用postgresql,我对它不熟悉。这段代码可以工作,但我想知道我是否可以用更直接的方式来编写它。在这里,我将bar连接到子查询中的bar。我希望有一些简单的东西像select * from bar group by baz using max(z)

select *
from foo f
join bar b on(f.baz=b.baz AND b.z in (select max(z) from bar group by baz))
where uid1 = 120

最佳答案

只需使用distinct on

select distinct on (f.baz) *
from foo f join
     bar b
     on f.baz = b.baz
where uid1 = 120
order by f.baz, b.z desc;

关于sql - 在PostgreSQL中通过max(col)获取行过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39748563/

10-13 00:47