selecting max value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

selecting max value

Post by pelegk2 »

i want to make a select on :
id num
------- ----------
28431 222.3347
28451 222.3346
28452 502.2256

where i want to get the max(id) but it's "num" too !!!
i tried :
select max(id),num from xxx group by id
and it gave me all the rows and not just the max(id)

what can i do?
thnaks in advane
peleg
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post by wyred »

Are you using MySQL as your database? I don't know much about SQL statements but I hope this helps.

Code: Select all

SELECT id, num FROM tbl ORDER BY id DESC LIMIT 1
Basically it's selecting the 2 fields you want, ORDER BY id DESC so you get the highest id on top, and then LIMIT 1 makes it so it returns only 1 row.

I assume id is of integer type. If it's of string, I'm not sure if ORDER BY id DESC would work the way I expected it to be.

LIMIT works only in MySQL if I'm not wrong.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

nice trick:)

Post by pelegk2 »

and limit is in MYSQL i think
ianlandsman
Forum Newbie
Posts: 24
Joined: Thu Dec 30, 2004 9:50 pm
Location: New York

Post by ianlandsman »

Well I would probably just do this:

Code: Select all

SELECT num FROM table ORDER BY id DESC LIMIT 0,1
oh just realized that wyred posted this. oh well I'll post it anyway.
Post Reply