如何在 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)
廣告