last data in the database
Moderator: General Moderators
-
myharshdesigner
- Forum Commoner
- Posts: 43
- Joined: Sat Apr 21, 2007 8:23 pm
last data in the database
how to get last data in the database ?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
In the whole database? No clue. You mean from a specific table?
You'd use ORDER BY and LIMIT.
i.e.
You'd use ORDER BY and LIMIT.
i.e.
Code: Select all
select `columnName` from `tableName` order by `id` desc limit 1;- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
If you're using MySQL you could use:
assuming you have an auto-increment id field
or
Depending on the version of MySQL you are using you can use MAX() or GREATEST.
assuming you have an auto-increment id field
Code: Select all
SELECT *
FROM tbl
ORDER BY fieldId DESC
LIMIT 1Code: Select all
SELECT max(id) as lastrow FROM tbl- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
That would give you the latest id.aceconcepts wrote:Depending on the version of MySQL you are using you can use MAX() or GREATEST.Code: Select all
SELECT max(id) as lastrow FROM tbl
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
If you are not using MySQL (generally assumed you are if you do not mention any other) you may have to do something different. For Postgres you generally have to get the ID of what is termed a sequence before you create the table row (setting the table row id to the previously retrieved value). Whilst this may seem a pain, it is easy to do and has the advantage you can use the same sequence on multiple tables.