I have a database that looks like this:
ID parent name description _record_status _log_user _log_timestamp _log_type
--------------------------------------------------------------------------------------------------------------------
1 1 This is a rwo! Test content active 1 2012-01-29 15:49:21 create
2 1 This is a row! Test Content active 1 2012-01-29 15:52:14 revision
3 3 Another record! More content active 1 2012-01-29 15:58:43 create
4 4 Deleted Record More content active 1 2012-01-29 15:58:43 create
5 4 Deleted Record More content deleted 1 2012-01-29 15:58:43 destroy
I want to be able to select the newest row for each record, where the record isn’t deleted. So for example, the output I’d expect is:
ID name
------------------
2 This is a row!
3 Another Record!
Is there a way to do this via SQL that is efficient, and if not, what might I want to do in PHP to accomplish this?
Would having a separate version of each table for revisions be the way to go here?
You’d get the highest record per
parentfirst, then excluded anything that has beendeleted:This can be slightly optimized if you know that
IDvalues are always in ascending time order. Then you could base theMAXrecord comparison onIDrather than a date and time value.