How to treat MySQL longtext as integer in MySQL query?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Value longtext
   -> );
Query OK, 0 rows affected (0.94 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('778437437447488487476464644433334');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('9998888485775775757577578585');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('8888874757757757757757575675656');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('988757757574646');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+-----------------------------------+
| Value                             |
+-----------------------------------+
| 778437437447488487476464644433334 |
| 9998888485775775757577578585      |
| 8888874757757757757757575675656   |
| 988757757574646                   |
+-----------------------------------+
4 rows in set (0.00 sec)

Here is the query to treat MySQL longtext as integer in query −

mysql> select * from DemoTable
   -> order by Value+0;

This will produce the following output −

+-----------------------------------+
| Value                             |
+-----------------------------------+
| 988757757574646                   |
| 9998888485775775757577578585      |
| 8888874757757757757757575675656   |
| 778437437447488487476464644433334 |
+-----------------------------------+
4 rows in set (0.03 sec)
raja
Published on 18-Dec-2019 06:39:21
Advertisements