ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a table with a number of columns and rows that are ordered by the variable artID. I need to get the last entry in the table, and I've been having trouble with this. I was using MAX() and was able to get the maximum artID, but I was unable to get the whole row. This is the statement I used, and by the way I'm using mysql:
SELECT MAX(artID) AS artID FROM articles;
I'm going to need to select the last row, and then the second to last row, and then the third to last row, until I get to the eighth row any ideas? It would be easy if I could use a variable in the sql statement, but I don't think I can. I also think that it'll be easier if I can get each row one at a time. I'd appreciate any thoughts on this, as I'm stumped.
to get the last row of a table, I found I could do it like this: -
Code:
select * from articles order by artid desc limit 1;
It basically lists all the rows in decreasing artid order, and only select the first 1 (which is the last row in the table as it's reversed). To get the second from last: -
Code:
select * from articles order by artid desc limit 1,1;
when limit is given two params, it uses the first number as an offset, and the second number as the number of rows to get. If you wanted you could get all last 8 rows by saying .....limit 0,8.
Retrieve the last row of a table for MS SQL database 2005
Quote:
Originally Posted by oulevon
I have a table with a number of columns and rows that are ordered by the variable artID. I need to get the last entry in the table, and I've been having trouble with this. I was using MAX() and was able to get the maximum artID, but I was unable to get the whole row. This is the statement I used, and by the way I'm using mysql:
SELECT MAX(artID) AS artID FROM articles;
I'm going to need to select the last row, and then the second to last row, and then the third to last row, until I get to the eighth row any ideas? It would be easy if I could use a variable in the sql statement, but I don't think I can. I also think that it'll be easier if I can get each row one at a time. I'd appreciate any thoughts on this, as I'm stumped.
This may be a new record----this thread is 7 1/2 years old.
Deepali;
I'd be willing to bet that the OP is no longer around. It is generally not a good idea to dig up old threads like this.
BUT---Welcome to LQ!!
Perhaps generally not so, but when google sends me in to a thread matching my issue with a tardy, correct response, it increases the worth of LQ tenfold.
Just my two cents. This site has been a shining star for as long as I can remember using Linux. Thank you to the moderators that turn the noise into signal.
The standard SQL method is to us a WHERE clause to find specific rows in a table. That's primarily because SQL tables are not, normally, ordered in any fixed sense, and questions like "find the last row" are almost always meaningless without a precise definition of what is meant by the word "last" in the question. The WHERE clause is usually that precise definition.
So, for the question first poised by oulevon, where the id of the record that was desired was indicated to be SELECT MAX(artID) AS artID FROM articles, the "standard" SQL select would be
SELECT * from articles A WHERE A.artID = (SELECT MAX(artID) FROM articles);
Depending on the syntax preferred by the specific implementation of SQL being used, the local alias of for the "articles" table (the "A" may differ from that above, but the use of the nested SELECT to define the row is, generally, the preferred approach.
<edit>
And I wasn't even a LQ member 7 years ago . . .
</edit>
Last edited by PTrenholme; 04-02-2009 at 10:47 AM.
Use a natural key instead of a physical locator and there won't be a problem to solve. That's the difference between logic and physical design actually. Relational databases exist to abstract the data from the physical storage details/mechanisms. What does max(artID) really represent? Probably the last article to be published or reviewed or something like that, which usually means searching on a date/time field.
To quote the always diplomatic and eloquent Mr. Joe Celko:
Quote:
Technically, these are not really keys at all, since they are attributes of the physical storage and are not even part of the logical data model. But they are handy for lazy, non-RDBMS programmers who do not want to research or think! This is the worst way to program in SQL.
Every program has at least one bug and can be shortened by at least one instruction
-- from which, by induction, one can deduce that every program can be reduced to one instruction which doesn't work.
Nonsense. There was no "stop" rule in that statement, so we can remove that last instruction leaving nothing, which can not contain one instruction that can not work. Thus the perfect program is an empty program, and we welcome all programmers to the beauty of Zen.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.