0

我有fixedassetsfixedassets_history桌子。

表中fixedassetsid, purchase_date, purchase_price, updated_date

fixedassets_history表中,有fixedassets_id, description, updated_date

我将这些表与id. 我想根据MySQL 中fixedassets的 'update_date字段和fixedassets_history'从这些表中检索最近更新的记录。updated_date

这是我的 SQL 脚本:

SELECT fa.*,fh.text,fh.date  
FROM fixedassets fa 
JOIN fa_history fh ON fa.id=fh.fixedassets_id 
ORDER BY fa.updated_date desc,fh.date desc  limit 5
4

1 回答 1

0
select 
fa.id,
max(fa.updated_date) , 
fa.other_column
from fixedassets fa 
left join (
            select 
                fixedassets_id , 
                max(updated_date) , 
                other_columns 
                from fixedassets_history
            ) as fah on fah.fixedassets_id = fa.id
group by fa.id
于 2012-08-17T05:07:08.913 回答