在 MySQL 中将 varchar 转换为无符号整数

mysqlmysqli database更新于 2025/11/4 22:36:17

要将 varchar 转换为无符号整数,请使用 CAST() 函数。让我们首先创建一个表 −

mysql> create table DemoTable868(Amount varchar(100));
Query OK, 0 rows affected (0.51 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable868 values('781');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable868 values('990');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable868 values('1002');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable868 values('560');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable868 values('890');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable868 values('9890');
Query OK, 1 row affected (0.06 sec)
mysql> insert into DemoTable868 values('8723');
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable868;

这将产生以下输出 −

+--------+
| Amount |
+--------+
| 781    |
| 990    |
| 1002   |
| 560    |
| 890    |
| 9890   |
| 8723   |
+--------+
7 rows in set (0.00 sec)

以下是将 varchar 转换为无符号整数的查询 −

mysql> select cast(Amount as unsigned) AS Amount from DemoTable868 order by Amount DESC;

这将产生以下输出 −

+--------+
| Amount |
+--------+
| 9890   |
| 8723   |
| 1002   |
| 990    |
| 890    |
| 781    |
| 560    |
+--------+
7 rows in set (0.01 sec)

相关文章


有用资源