如何比较两个字符串在MySQL中是数字?

要比较MySQL中的两个数字字符串,请使用CAST()函数。

语法如下

select *from yourTableName
where cast(yourColumnName as signed)=yourIntegerValue;

为了理解上述语法,让我们创建一个表。创建表的查询如下

create table compareTwoStringDemo
   -> (
   -> UserId varchar(100)
   -> );

使用insert命令在表中插入一些记录。查询如下-

insert into compareTwoStringDemo values('1083745');
insert into compareTwoStringDemo values('9867585');
insert into compareTwoStringDemo values('3547483');
insert into compareTwoStringDemo values('9845646');
insert into compareTwoStringDemo values('9876532');

使用select语句显示表中的所有记录。查询如下-

select *from compareTwoStringDemo;

以下是输出

+---------+
| UserId  |
+---------+
| 1083745 |
| 9867585 |
| 3547483 |
| 9845646 |
| 9876532 |
+---------+
5 rows in set (0.00 sec)

这是比较两个数字字符串的查询

select *from compareTwoStringDemo
   -> where cast(UserId as signed)=3547483;

以下是输出

+---------+
| UserId  |
+---------+
| 3547483 |
+---------+
1 row in set (0.00 sec)