select latest row in mysql but primary is varchar

Cendol404 :

I have a mysql table like this ( pid is varchar ):

| pid | name |
| p1  | a    |
| p10 | b    |
| p11 | c    |
| p2  | d    |
| p3  | e    |

I want to select latest inserted row ( p11 ), but the selected one is p3, while I want to select p11. I have tried using this query:

SELECT MAX(pid) as LASTID FROM product

How can I write a query to return p11?

Barmar :

Remove the p and convert it to a number before calculating the maximum.

SELECT CONCAT('p', MAX(REPLACE(pid, 'p', '') + 0)) AS lastid
FROM product

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216472&siteId=1