selecting record which has a field with highest value

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
megaming
Forum Commoner
Posts: 27
Joined: Wed Dec 25, 2002 12:31 pm

selecting record which has a field with highest value

Post by megaming »

Ok,

I want to select a record from a mysql database table, and to determine the record, I want to look in the field for each record and find which has the highest value.

Hope this isn't too confusing, thanks.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

If I understood it right, you should manage this by using something like:

Code: Select all

select * from table where id = max(id) limit 1
'max(id)' would give you the highest number of id (usually an auto_incr. number, change to fit your needs), combined with 'limit 1' that narrows down the hits to one row of results.

Hope that helped some.
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Post by mwong »

Maybe you could also order by the id field and put a DESC in there and limit it by 1 :D
Post Reply