I know how to pull up the last entry.
Code: Select all
SELECT *
FROM Person
ORDER BY EntryDate
DESC LIMIT 1Moderator: General Moderators
Code: Select all
SELECT *
FROM Person
ORDER BY EntryDate
DESC LIMIT 1Code: Select all
SELECT *
FROM Person
ORDER BY EntryDate DESC
LIMIT 1, 1Code: Select all
SELECT *
FROM Person
ORDER BY EntryDate DESC
LIMIT 1, 1Yes, an order column is only used in place of "implicit" ordering. ( For ex if the rows are inserted in a certain order doesnt mean a select will return them in that order, always use an ORDER BY clause in the query if order is important, which you did which is good )edawson003 wrote: I am a bit reluctant to create an order column, cuz now that is one extra field I have to code for. Couldn't I just utilize the entry date field as a way to delineate order?
This may be an issue of interpretation. You are saying (and thinking, I'm sure) that you want to retrieve the "2nd last entry in my table", but that's NOT what you are actually doing at all. That language implies that you are speaking of the order in which the data was entered, and presumably is physically stored. That concept has no meaning in relational databases. What I believe you actually mean is the next-to-maximum date in the Person table, which is entirely different. That is what your query, above, is selecting. It has nothing to do with "where" it is stored in the table. So you are doing the right thing, but you are describing it poorly.edawson003 wrote:Actually, I tested the below code and it returned the 2nd last entry in my table. The fact that I order by entry date (which is a datetime) in descending order allowed for the "LIMIT" to pull from the bottom, so to speak.I am trying to wrap my head around the "adding an order" column suggestion. I am a bit reluctant to create an order column, cuz now that is one extra field I have to code for. Couldn't I just utilize the entry date field as a way to delineate order? Could you please elaborate why select MAX( `order` ) from `table` where 1 would be a better solution?Code: Select all
SELECT * FROM Person ORDER BY EntryDate DESC LIMIT 1, 1
Just for my own edification, under what circumstances, would basing the logic of an application where a row is located in the table produce upredictable results? Even with the ORDER BY EntryDate DESC, do you still think it could end up messy?