Jonob Posted February 19, 2009 Share Posted February 19, 2009 Hi all, Assuming I have the following structure table: company company_id company_name table: status status_id company_id (FK) status_date description How do I get all the records from company, and only the single most recent record (by status_date) from status? I want to show all fields from both tables, and if there is no record in status, then it should return nulls for each field in status. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/146039-solved-select-most-recent-record-from-child-table/ Share on other sites More sharing options...
angelcool Posted February 20, 2009 Share Posted February 20, 2009 You will have to use a JOIN query: SELECT * FROM compay JOIN status ON company.company_id=status.company_id and to get the most recent record: SELECT * FROM compay JOIN status ON company.company_id=status.company_id ORDER BY status_date LIMIT 1 Good luck. NaCo Quote Link to comment https://forums.phpfreaks.com/topic/146039-solved-select-most-recent-record-from-child-table/#findComment-766887 Share on other sites More sharing options...
angelcool Posted February 20, 2009 Share Posted February 20, 2009 Missed the DESC. SELECT * FROM compay JOIN status ON company.company_id=status.company_id ORDER BY status_date DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/146039-solved-select-most-recent-record-from-child-table/#findComment-766889 Share on other sites More sharing options...
Jonob Posted February 20, 2009 Author Share Posted February 20, 2009 Just in case anyone wants to know in the future, this is what got me the right answer: SELECT c.company_id, c.company_name, t1.* FROM company c LEFT JOIN (select max(status_date) as date, status_id, description, company_id from status group by company_id) as t1 ON c.company_id = t1.company_id Quote Link to comment https://forums.phpfreaks.com/topic/146039-solved-select-most-recent-record-from-child-table/#findComment-767217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.