Reputation: 31252
I have a column version
which is varchar
but contains double
values.
when I try to get the latest (max) version I get incorrect results. But when I do min, I get correct result.
How is the ordering of varchar columns containing double determined?
Here is the content of my table;
mysql> Desc Application;
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| version | varchar(50) | YES | | NULL | |
+---------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> select * from Application;
+----+---------+
| id | version |
+----+---------+
| 1 | 0.0.0 |
| 2 | 0.0.1 |
| 3 | 0.0.2 |
| 4 | 0.1.0 |
| 5 | 0.1.1 |
| 6 | 0.1.2 |
| 7 | 1.0.0 |
| 8 | 1.0.1 |
| 9 | 1.1.1 |
| 10 | 8.7.3 |
| 11 | 10.29.0 |
+----+---------+
11 rows in set (0.00 sec)
select max(version) from application;
+--------------+
| max(version) |
+--------------+
| 8.7.3 |
+--------------+
1 row in set (0.00 sec)
mysql> select version from application
-> ORDER BY version DESC
-> LIMIT 1;
+---------+
| version |
+---------+
| 8.7.3 |
+---------+
1 row in set (0.00 sec)
mysql> select min(version) from application;
+--------------+
| min(version) |
+--------------+
| 0.0.0 |
+--------------+
1 row in set (0.00 sec)
Upvotes: 0
Views: 160
Reputation:
Forget the idea that these are floats. MySQl sees them as character strings because that's what you've told it.
Sorting by character order, 8
is the largest, and 0
is the minimum, so your results are correct.
If you want MySQL to sort by numerical order, give it numerical fields. Alternatively, right-align and zero-pad your subfields - 08.07.03
is less than 10.29.00
Upvotes: 4