The easiest way is to use group_concat()with substring_index():
select i.item_id, i.user_id, i.data,
substring_index(group_concat(id.detail order by id.id desc), ',', 1
) as last_detail,
substring_index(group_concat(case when id.ignored = 0 then id.detail1 end order by id.id desc), ',', 1
) as last_non_ignored_detail1
substring_index(group_concat(case when id.ignored = 0 then id.detail2 end order by id.id desc), ',', 1
) as last_non_ignored_detail2
substring_index(group_concat(case when id.ignored = 0 then id.detail3 end order by id.id desc), ',', 1
) as last_non_ignored_detail3
substring_index(group_concat(case when id.ignored = 0 then id.detail4 end order by id.id desc), ',', 1
) as last_non_ignored_detail4
from items i join
item_detail id
on i.item_id = id.item_id
group by i.item_id;
source
share