MauriceW
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Kandura gave you the right answer :) Listen to him
Cheers
Cheers
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