Last row in a table

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
How would be the best way to fetch the last row in a MySQL table?
 
I can find only one reason why you want the last row, and that's to find out wich id was added with the last insert query.
so, I think you're looking for this:
Code:
$inserted_id = mysql_insert_id($query_result);

mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.

Greetz Daan
 
Xerpher said:
How would be the best way to fetch the last row in a MySQL table?
That would depend on how your table is sorted too! Just select one row and sort the 'sort' column in descending order!

If if you're after the last row literally, you could count how many rows there are and then use (at the end of the query)
limit (totalRows-1),1

where totalRows is how many rows there are, and 1 is how many rows it'll display after that!
 
DoppyNL said:
.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan

8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database. So:
=============================
while ($db_obj=mysql_fetch_object($result))
{
$last_element=$db_obj->some_field;
}
=============================
you have the some_field from the last row!

------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
http://www.alarit.com
 
serge said:
DoppyNL said:
.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan

8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database.

mysql DOES NOT return the recordset the way it was entered in the database!!!!
when you haven't deleted any records you may get what you expect, but when you deleted some rows and/or are working with records of a variable length and insert some after that you WILL get some other order wich is NOT the order you entered the records!!

Greetz Daan
 
DoppyNL said:
serge said:
DoppyNL said:
.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan

8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database.

mysql DOES NOT return the recordset the way it was entered in the database!!!!
when you haven't deleted any records you may get what you expect, but when you deleted some rows and/or are working with records of a variable length and insert some after that you WILL get some other order wich is NOT the order you entered the records!!

Greetz Daan
:twisted: But it is order as records stored in database!
The question was:
How would be the best way to fetch the last row in a MySQL table?
------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com
 
serge said:
:twisted: But it is order as records stored in database!
That's true, but you don't know what order that is :D :mrgreen: :!:
 
I repeat:
The task is to find LAST ROW
:!:
------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com
 
Back
Top