简体   繁体   中英

how to select the max value in mysql in varchar field

I have one mysql table, in that table contain varchar field. I want to get the max value from the table.

My Table - 在此处输入图片说明

My SQL QUERY -

SELECT Max(CAST(test as SIGNED)) as a FROM testtable;

Wanted Output-

G450

how to select the max value in mysql in varchar field?

If your data always has single character followed by integer, you can use the following:

SELECT * FROM testtable WHERE substring(test,2)=(SELECT MAX(CAST(SUBSTRING(test,2) AS SIGNED)) FROM testtable);

This will return all rows with the character followed by the max integer - ie if you have G540 and X540, it'll return both.

SELECT CONCAT('G' , MAX(0+SUBSTRING(test,2))) FROM TABLE_NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM