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)
阅读更多:MySQL 教程