With a custom function such as alphas
found here, you can order it by first the number portion of the value followed by the string portion of the value.
If you define and populate a table as such:
CREATE TABLE test (t VARCHAR(255));
INSERT INTO test VALUES
('10A'),
('2'),
('2A'),
('4'),
('10'),
('1');
Then create a custom function called alphas
which extracts the string portion (no numbers):
DELIMITER |
DROP FUNCTION IF EXISTS alphas;
CREATE FUNCTION alphas( str CHAR(32) ) RETURNS CHAR(16)
BEGIN
DECLARE i, len SMALLINT DEFAULT 1;
DECLARE ret CHAR(32) DEFAULT '';
DECLARE c CHAR(1);
SET len = CHAR_LENGTH( str );
REPEAT
BEGIN
SET c = MID( str, i, 1 );
IF c REGEXP '[[:alpha:]]' THEN
SET ret=CONCAT(ret,c);
END IF;
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;
Then you can do a ordered query like this:
SELECT t FROM test ORDER BY CAST(t AS UNSIGNED), alphas(t);
The CAST
function converts strings like 10A
to an unsigned number of 10
.