coo12
coo12

Reputation: 117

Get highest Value from MySQL Database

I want to get the highest value from a column of a MySQL Table:

example:

Code (PK)   | ...
AT10000
DE10001
FR10002

How I can get the Value "10002"?

Upvotes: 0

Views: 99

Answers (2)

Joël Salamin
Joël Salamin

Reputation: 3576

If you want to get the entire row corresponding to the max value, try this:

SELECT TOP 1 *
FROM yourTable T
ORDER BY SUBSTRING(T.code, 3) DESC

Or you also can use this one:

SELECT *
FROM yourTable T
WHERE SUBSTRING(T.code, 3) = (SELECT MAX(SUBSTRING(T2.code, 3)
                              FROM yourTable T2)

And this last one if you just want to have the max value without info about the row:

SELECT MAX(SUBSTRING(T.code, 3) AS [value]
FROM yourTable T

Hope this will help you.

Upvotes: 0

juergen d
juergen d

Reputation: 204746

select max(substring(code, 3) + 1)
from your_table

Upvotes: 5

Related Questions