Help - Search - Members - Calendar
Full Version: last row
HTMLHelp Forums > Programming > Databases
bunny1
I'm trying to get the value of the primary key for the last row in the database.
The Product_ID field is auto-incremented at every entry and i want to find out what the biggest Product_ID in the table is.

I'm using this sql code but it doesnt work

SELECT *
FROM 'Prods'
WHERE [ `Product_ID` = last];

any suggestions?

thanks
Brian Chandler
QUOTE
SELECT *
FROM 'Prods'
WHERE [ `Product_ID` = last];



Hmm, why do you think this should work? Is there something in the manual about being able to write this? (What do square brackets mean? Is Product_ID a field name? And if so, what does writing it in quotes mean, if not simply the string 'Product_ID'?)

Usually the way to get the last something is to select just the first something, with the order reversed. Something like*:

SELECT * FROM mytable ORDER BY myfield DESC;

* This "like" means I haven't checked the syntax in the manual, so you need to. It is completely hopeless trying to write database (or any other) programs by just guessing something you think the computer might just happen to understand.
Effovex
last_insert_id is, I think, a MySQL exclusive.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.