VirtualOdin Posted October 25, 2009 Share Posted October 25, 2009 I have two tables, 'person' with all the static data and 'event' with multiple records for each bit of dynamic data associated with rows in 'person' through 'person_id'. So I can extract all the event data with something like SELECT * FROM `person` LEFT JOIN `event` ON ( `person`.`id` = `event`.`person_id`) WHERE ... And from that result I can work out in PHP which is the latest event, which for this purpose is all I need. But I feel sure it should be possible to do all that inside the query and pull out only the latest 'event' data and the person material. Is it possible? As always, thanks in inticipation... Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/ Share on other sites More sharing options...
Mchl Posted October 25, 2009 Share Posted October 25, 2009 What is 'latest' in this case? Is there any timestamp field in the table? Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943953 Share on other sites More sharing options...
VirtualOdin Posted October 25, 2009 Author Share Posted October 25, 2009 Yes, sorry should have said, there is a DATE field in the 'event' table Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943954 Share on other sites More sharing options...
kickstart Posted October 25, 2009 Share Posted October 25, 2009 Hi Try something like this:- SELECT * FROM `person` a LEFT JOIN (SELECT person_id, max(eventDate) AS maxEventDate FROM `event` GROUP BY person_id) b ON a.id = `b.person_id JOIN `event` c ON b.person_id = c.person_id AND b.maxEventDate = c.eventDate WHERE ... All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943957 Share on other sites More sharing options...
VirtualOdin Posted October 25, 2009 Author Share Posted October 25, 2009 Thanks very much. It works. Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943963 Share on other sites More sharing options...
VirtualOdin Posted October 25, 2009 Author Share Posted October 25, 2009 That's great for one additional table. What I should have thought through is that I have a four tables like 'event' on each of which I need to do the same. I'd appreciate any advice on how to do it with more than one, so for each person I pull out the latest value from the other tables organised like event, ie with a date and some date related to the person (max dates will vary of course). I can't quite see what to replicate to end up with all the latest data from the 4 tables. Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943982 Share on other sites More sharing options...
VirtualOdin Posted October 25, 2009 Author Share Posted October 25, 2009 So trial and error has produced this for two tables SELECT * FROM `person` a LEFT JOIN (SELECT `person_id`, MAX(`range`.`date`) AS maxRangeDate FROM `range` GROUP BY `person_id`) b ON (`a`.`id` = `b`.`person_id`) LEFT JOIN (SELECT `person_id`, MAX(`assignment`.`date`) AS maxAssignmentDate FROM `assignment` GROUP BY `person_id`) d ON (`a`.`id` = `d`.`person_id`) JOIN `range` c ON ( `b`.`person_id` = `c`.`person_id` AND `b`.`maxRangeDate` = `c`.`date`) JOIN `assignment` e ON ( `b`.`person_id` = `e`.`person_id` AND `d`.`maxAssignmentDate` = `e`.`date` ) WHERE `a`.`departure` = '0000-00-00' But it is taking quite a time on no data to speak of. Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943986 Share on other sites More sharing options...
kickstart Posted October 25, 2009 Share Posted October 25, 2009 Hi Looks a fair enough way to do it. You do not need that many back tics (and I would recommend avoiding using any column names that are reserved words in SQL). You have one minor typo:- SELECT * FROM person a LEFT JOIN (SELECT person_id, MAX(`range`.`date`) AS maxRangeDate FROM range GROUP BY person_id) b ON (a.id = b.person_id) LEFT JOIN (SELECT`person_id, MAX(`assignment`.`date`) AS maxAssignmentDate FROM assignment GROUP BY person_id) d ON (a.id = d.person_id) JOIN range c ON ( b.person_id = c.person_id AND b.maxRangeDate = `c`.`date`) JOIN assignment e ON ( d.person_id = e.person_id AND d.maxAssignmentDate = `e`.`date` ) WHERE a.departure = '0000-00-00' After that I would suggest you check the indexing on those tables. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178921-extract-only-the-latest-value/#findComment-943988 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.