john john - 3 months ago 19
MySQL Question

Getting maximum column value in mysql

Basically I have:

TableName
___________________________________________________________
| Amount | ID | Phone VARCHAR(45) |
-------------------------------------------------------------
| 1 | Jack | 780799870 |
| 2 | fob | 120110117116105000137000000000000001|
| 3 | AliClay | 178234791827498789 |
-------------------------------------------------------------


I want to get the maximum value of my phone column which is
120110117116105000137000000000000001


but when i try
SELECT MAX(PHONE) FROM TableName;
I happen to get
780799870
as my result,

Answer

You are storing the value as a string -- which isn't surprising given that the numbers are so long.

Here is a simple way to get the record with the maximum value, assuming that the numbers never start with leading zeros (that can easily be included):

select t.*
from tablename t
order by length(phone) desc, phone desc;