Here a little sample of use SUBSTRING_INDEX and VIRTUAL COLUMN
Sample: show sorted rows of a small table
MariaDB [yourschema]> SELECT * FROM l ORDER BY substring_index(LEVEL,' ',-1)+0;
+----+----------+
| id | level |
+----+----------+
| 1 | Grade 1 |
| 3 | Grade 2 |
| 2 | Grade 10 |
+----+----------+
3 rows in set (0.04 sec)
Show table structure
MariaDB [yourschema]> show create table l;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| l | CREATE TABLE `l` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`level` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Add a virtual persistent column:
MariaDB [yourschema]> ALTER TABLE l ADD level_int INT AS (SUBSTRING_INDEX(`level`,' ',-1)) PERSISTENT;
Query OK, 3 rows affected (1.90 sec)
Records: 3 Duplicates: 0 Warnings: 0
Add new rows:
MariaDB [yourschema]> insert into l (level) VALUES ('Grade 23'),('Grade 132');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
Show all rows andd see the field level_int
:
MariaDB [yourschema]> SELECT * FROM l ORDER BY substring_index(LEVEL,' ',-1)+0;
+----+-----------+-----------+
| id | level | level_int |
+----+-----------+-----------+
| 1 | Grade 1 | 1 |
| 3 | Grade 2 | 2 |
| 2 | Grade 10 | 10 |
| 4 | Grade 23 | 23 |
| 5 | Grade 132 | 132 |
+----+-----------+-----------+
5 rows in set (0.00 sec)
Now you can add a index and query on the new column:
MariaDB [yourschema]> alter table l add index idx_level_int (level_int);
Query OK, 5 rows affected (0.93 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [yourschema]>