在INT字段上执行MySQL LIKE比较?
您需要使用cast()
方法对INT字段执行比较。语法如下-
SELECT yourColumnName1,yourColumnName2,......N yourTableName WHERE CAST(yourColumnName as CHAR) LIKE ‘%yourIntegerValue%’;
为了理解上述语法,让我们创建一个表。以下是创建用于对INT字段执行LIKE比较的表的查询-
mysql> create table ComparisonOnIntField -> ( -> StudentId int NOT NULL, -> StudentName varchar(20), -> StudentAge int -> );
在表中插入一些记录,以在INT字段上执行MySQLLIKE比较。插入记录的查询如下-
mysql> insert into ComparisonOnIntField values(10,'Carol',24); mysql> insert into ComparisonOnIntField values(12,'Bob',21); mysql> insert into ComparisonOnIntField values(14,'Sam',23); mysql> insert into ComparisonOnIntField values(16,'Mike',25); mysql> insert into ComparisonOnIntField values(18,'John',27); mysql> insert into ComparisonOnIntField values(20,'David',26);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from ComparisonOnIntField;
以下是输出。
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 10 | Carol | 24 | | 12 | Bob | 21 | | 14 | Sam | 23 | | 16 | Mike | 25 | | 18 | John | 27 | | 20 | David | 26 | +-----------+-------------+------------+ 6 rows in set (0.00 sec)
这是在INT字段上执行MySQLLIKE比较的查询-
mysql> select StudentName,StudentAge from ComparisonOnIntField -> where cast(StudentId as CHAR) Like '%18%';
以下是输出-
+-------------+------------+ | StudentName | StudentAge | +-------------+------------+ | John | 27 | +-------------+------------+ 1 row in set (0.05 sec)