How to get the last 15 records from a MySQL table in the right order

Dear Experts,

I have a MySQL table called "messages" which has two fields : name and message.

Now I need an example how to query this table and only fetch the LAST 15 records in (I guess) reverse order.

For example, when I have 1500 records:
The result must be one string containing record 1486, 1487, 1488 and so on: FIELD "name" + "some text" + field "MESSAGE" + " some text".

The idea is that people can enter a name and some short text in two fields on a webpage. Those fields are stored correctly into the table.
Now I need to display only the last 15 messages in chronological order, so last placed message last.

Kind regards,

Maurice
MauriceWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

holliCommented:
using MS-ACCESS you can have a "LAST n"-statement in your SQL-statement.
that is not possible with mysql.

you will have to use a "counter" field wich value you can use in your statement ( eg. SELECT ... WHERE counter >= 1445 and counter < 1500) or fetch all records in an array and get the last 15 out of these
0
kanduraCommented:
I hope you either have a date field, or a primary key field, otherwise sorting will be impossible.

Assuming you have an auto_increment primary key, you'd do:
    select name, message from messages order by message_id desc limit 15

If instead you have a create_date field:
    select name, message from messages order by create_date desc limit 15

These statements will select the last 15 records, but not in the order you want them. Since it's only 15, I suggest fetching them all, and reversing the result set in perl:

my @messages;
my $sth = $dbh->prepare('select name, message from messages order by message_id desc limit 15 ');
$sth->execute();
while(my $r = $sth->fetchrow_hashref) {
    push @messages, $r ;
}

foreach (reverse @messages) {
    print "Name: ", $_->{name}, $/;
    print "Message: ",  $_->{message}, $/;
}



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rugdogCommented:
AFAIK,  if no order clause is specified in a MySQL query, the order the rows  are retrieved is the order they were inserted, so if you need to fetch the last 15, try this:

1. compute the total number of rows
  select count(*) from mesages;
2. substract 15 to the count minus one (since first row is 0) and use that number in limit, so, for example, if the count is 100 try:

select * from messages limit 84,15
0
gruntarCommented:
Kandura gave you the right answer :) Listen to him

Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.