Page 1 of 1

How to fetch last field of the recordset

Posted: Wed May 26, 2004 5:30 am
by ankurdave
HI to all
My problem is

I have one table in which one field is id which is having some numbers from 1 to 100 some diff . no. now my qurey is

Code: Select all

select * from ABC order by id
so the table is maintained by the id.

Now i want to fetch only the last id


I simply want to fetch the last id valus so that i can assign the new value in id which is higher then the previous one.


Thnx in ad.

Posted: Wed May 26, 2004 5:49 am
by JayBird
if you wanna select the last id in the database, you could use this query

Code: Select all

SELECT MAX(id) FROM abc
Mark

Posted: Wed May 26, 2004 6:32 am
by kevin7
or... u can try...

Code: Select all

SELECT * FROM ABC order by id desc

Posted: Wed May 26, 2004 9:59 am
by launchcode
SELECT MAX(id) FROM abc
Depending on his table structure, that might not always work. MySQL can often re-use old values - i.e. it might not always be the largest number that was the most recent addition. If he's got ID set to be an auto-inc field, he should be fine :) but you never know...

Posted: Wed May 26, 2004 10:01 am
by malcolmboston
jus to give you an idea

Code: Select all

$result = "// the query";
$num_rows = mysql_num_rows($result);
// now we have last row
// do for loop?? to get last number
mysql_fetch_row???

Posted: Wed May 26, 2004 10:10 am
by ankurdave
Thnx I got the ans by your views

Thnx to all

Posted: Wed May 26, 2004 10:17 am
by JayBird
launchcode wrote:
SELECT MAX(id) FROM abc
Depending on his table structure, that might not always work. MySQL can often re-use old values - i.e. it might not always be the largest number that was the most recent addition. If he's got ID set to be an auto-inc field, he should be fine :) but you never know...
But as he said "I simply want to fetch the last id valus so that i can assign the new value in id which is higher then the previous one." i thought this would suffice. He just want to assign a new unique value from what i understand by the information he gave us.

Mark

Posted: Wed May 26, 2004 10:20 am
by Wayne
also if they were using AUTO INCREMENT they wouldn't need to do all of this to get the last id out, it would be done for them. stick with the MAX().