- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort data column to retrieve max textual value in MySQL
For this, you can use ORDER BY along with some aggregate function right(). Let us first create a table −
mysql> create table DemoTable1487 -> ( -> StudentCode text -> ); Query OK, 0 rows affected (0.91 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1487 values('560'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable1487 values('789'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1487 values('STUDENT78'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1487 values('John89'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1487 values('Carol101'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1487 values('STUDENT98'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1487;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | 560 | | 789 | | STUDENT78 | | John89 | | Carol101 | | STUDENT98 | +-------------+ 6 rows in set (0.00 sec)
Here is the query to sort data column to retrieve maximum textual value −
mysql> select * from DemoTable1487 where StudentCode LIKE 'STUDENT%' -> order by cast(right(StudentCode,length(StudentCode)-length('STUDENT')) as UNSIGNED) desc -> limit 1;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | STUDENT98 | +-------------+ 1 row in set (0.04 sec)
Advertisements