Hey, I have 2 tables in PostgreSql:
1 - documents: id, title
2 - updates: id, document_id, date
and some data:
documents:
select *
from documents
left join updates
on updates.document_id=documents.id
and updates.date=(select max(date) from updates where document_id=documents.id)
where documents.id=?;
It has the some advantages over previous answers:
where documents.id in (1,2,3)
.You can also avoid a subselect using group by, but you'll have to list all fields of documents in group by clause:
select documents.*, max(date) as max_date
from documents
left join updates on documents.id=document_id
where documents.id=1
group by documents.id, title;