Never mind, I think I came up with a pretty elegant solution! :cool:
(Good thing I didn't post my cheap trick.)
create table if not exists data(id int, title varchar(30));
create table if not exists other(data_id int, datefield datetime, otherinfo varchar(30));
insert into data(id, title) values
(1, 'title 1'), (2, 'title 2');
insert into other(data_id, datefield, otherinfo) values
(1, '2001-01-01', 'older row for 1'),
(1, '2002-01-01', 'newer row for 1'),
(2, '2001-01-01', 'only row for 2');
select distinct data.id, other.otherinfo, other.datefield
from data
left join other on data.id = other.data_id
left join (
select data_id, max(datefield) latest_date
from other
group by data_id
) rep on data.id=rep.data_id and other.datefield=rep.latest_date
where not (other.data_id is not null and rep.data_id is null)
Let me know if you need me to explain.
Thanks,
Thomas