如何在 MySQL 中比较两个数字字符串?
若要比较 MySQL 中的两个数字字符串,请使用 CAST() 函数。
语法如下
select *from yourTableName where cast(yourColumnName as signed)=yourIntegerValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table compareTwoStringDemo -> ( -> UserId varchar(100) -> ); Query OK, 0 rows affected (0.78 sec)
使用插入命令在表中插入一些记录。查询如下 −
mysql> insert into compareTwoStringDemo values('1083745');
Query OK, 1 row affected (0.12 sec)
mysql> insert into compareTwoStringDemo values('9867585');
Query OK, 1 row affected (0.11 sec)
mysql> insert into compareTwoStringDemo values('3547483');
Query OK, 1 row affected (0.15 sec)
mysql> insert into compareTwoStringDemo values('9845646');
Query OK, 1 row affected (0.15 sec)
mysql> insert into compareTwoStringDemo values('9876532');
Query OK, 1 row affected (0.10 sec)使用 select 语句从表中显示所有记录。查询如下 −
mysql> select *from compareTwoStringDemo;
以下是输出
+---------+ | UserId | +---------+ | 1083745 | | 9867585 | | 3547483 | | 9845646 | | 9876532 | +---------+ 5 rows in set (0.00 sec)
以下是比较两个数字字符串的查询
mysql> select *from compareTwoStringDemo -> where cast(UserId as signed)=3547483;
以下是输出
+---------+ | UserId | +---------+ | 3547483 | +---------+ 1 row in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP