On Saturday, January 29, 2011 at 3:03 PM, Marian Cascaval wrote: > Here's the info source on LAST() function: > > http://www.w3schools.com/sql/sql_func_last.asp > > > > I needed to retrieve the last row from a table. > And the example (the workaround tip) in the above link solved my issue. > > > >
While the "workaround" solved your problem, the workaround *is* the right way to solve the problem. Their original solution is absolutely goofy. From their docs, "The LAST() function returns the last value of the selected column." That sentence makes absolutely no sense. What does "last value of the selected column" mean? We happen to now know that they mean the last row in the result set. But, why not just say that? Their proposed solution is absolutely out of whack -- SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders Note that a SQL result set doesn't have a concept of first or last unless you impose an order on it using the ORDER BY clause. Interestingly, they don't say which databases support this LAST() function. I have not come across, yet, any database that supports a LAST() function the way they say it should. In other words, rest assured, LIMIT 1 is indeed the correct way to restrict the result set to one row. However, if you want the "last" row, you have to tell your database program what you mean by last. You do so by specifying ORDER BY <column or expression>. Then, you can take the last or the first by using LIMIT properly. For example, if the orders were to be ordered by, say, the OrderDate, and you wanted the earliest date, you could do SELECT OrderPrice FROM Orders ORBER BY OrderDate LIMIT 1; If you wanted the latest order, you could do SELECT OrderPrice FROM Orders ORDER BY DESC OrderDate LIMIT 1; Hope this helps. > > ________________________________ > From: Puneet Kishor <punk.k...@gmail.com> > To: General Discussion of SQLite Database <sqlite-users@sqlite.org> > Sent: Sat, January 29, 2011 10:47:44 PM > Subject: Re: [sqlite] LAST() function not supported > > > > > On Saturday, January 29, 2011 at 2:38 PM, Marian Cascaval wrote: > > > > Hi! > > > > Is LAST() function going to be supported? > > > > Or will the "SELECT ... FROM ... ORDER BY ... DESC LIMIT 1" workaround > > always > > be > > > > enough? > > > > > > > Where did you get information on this "LAST()" function? > > > > > My concern is if there might be any speed improvement if LAST() function > > were > > to > > > > be implemented, comparing to the workaround. > > > > > You are possibly confusing how a function works vs. the SQL syntax. A > function > acts on a column or an expression for every row in the result set. It doesn't > modify the number of rows in a result set. On the other hand, the LIMIT > clause > does nothing to the entries that have been retrieved. Instead, it throttles > the > size of the result set, that is, it controls the number of rows in the result > set. > > > > > -- > Puneet Kishor > Sent with Sparrow > > > > > _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users