Page 1 of 1

Last row in a table

Posted: Sun Sep 01, 2002 8:08 pm
by Xerpher
How would be the best way to fetch the last row in a MySQL table?

Posted: Mon Sep 02, 2002 12:57 am
by DoppyNL
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: Select all

$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

Re: Last row in a table

Posted: Mon Sep 02, 2002 10:45 am
by Jay
Xerpher wrote: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!

Posted: Fri Sep 20, 2002 7:41 am
by serge
DoppyNL wrote:.....
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

Posted: Fri Sep 20, 2002 7:49 am
by DoppyNL
serge wrote:
DoppyNL wrote:.....
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

Posted: Fri Sep 20, 2002 9:23 am
by serge
DoppyNL wrote:
serge wrote:
DoppyNL wrote:.....
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

Posted: Fri Sep 20, 2002 9:26 am
by DoppyNL
serge wrote: :twisted: But it is order as records stored in database!

That's true, but you don't know what order that is :D :mrgreen: :!:

Posted: Fri Sep 20, 2002 9:30 am
by serge
I repeat:
The task is to find LAST ROW
:!:
------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com