Page 1 of 1

selecting max value

Posted: Fri Jan 14, 2005 2:35 am
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

Posted: Fri Jan 14, 2005 3:19 am
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.

nice trick:)

Posted: Fri Jan 14, 2005 3:31 am
by pelegk2
and limit is in MYSQL i think

Posted: Fri Jan 14, 2005 8:38 am
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.