SELECT MAX(DATE) FROM YOUR_TABLE
Above answer does not seem to satisfy OP requirements.
UPDATED RESPONSE AFTER INSERT / UPDATE TRIGGER
DELCARE @latestState varchar; DELCARE @latestDate date; CREATE TRIGGER latestInsertTrigger AFTER INSERT ON myTable FOR EACH ROW BEGIN IF OLD.DATE <> NEW.date THEN SET @latestState = NEW.state SET @latestDate = NEW.date END IF END ; CREATE TRIGGER latestUpdateTrigger AFTER UPDATE ON myTable FOR EACH ROW BEGIN IF OLD.DATE = NEW.date AND OLD.STATE <> NEW.STATE THEN SET @latestState = NEW.state SET @latestDate = NEW.date END IF END ;
You can use the following query to get the latest added / updated record:
SELECT DATE, STATE FROM myTable WHERE STATE = @latestState OR DATE = @latestDate ORDER BY DATE DESC ;
Results:
DATE STATE November, 30 2012 00:00:00+0000 state one November, 28 2012 00:00:00+0000 state two November, 27 2012 00:00:00+0000 state two
The above query results should be limited to 2, 3 or n based on what you need.
Honestly, it looks like you want to get max from both columns based on your sample data. Assuming your state only increases with the date. Only I would like state be integer : D Then combining the two maximum subqueries on both columns would resolve this easily. However, the regular expression for the row can find what is in the max column in the state. Finally, this approach requires limit x . However, it still has a “strong” hole tube. In any case, it took me a while to figure out what you need: $
source share