如何在MySQL中使用数字字符串进行比较运算符?
要使用数字字符串进行比较运算符,请使用substring()方法。让我们首先创建一个表 –
mysql> create table DemoTable1881
(
UserId int,
UserEducationGap varchar(20)
);
Query OK, 0 rows affected (0.00 sec)
使用插入命令在表中插入一些记录 –
mysql> insert into DemoTable1881 values(101,'5-9');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1881 values(102,'2-4');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1881 values(103,'4-8');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1881 values(104,'7-12');
Query OK, 1 row affected (0.00 sec)
使用select语句显示来自表中的所有记录 –
mysql> select * from DemoTable1881;
这将产生以下输出 –
+--------+------------------+
| UserId | UserEducationGap |
+--------+------------------+
| 101 | 5-9 |
| 102 | 2-4 |
| 103 | 4-8 |
| 104 | 7-12 |
+--------+------------------+
4 rows in set (0.00 sec)
这是在MySQL中使用数字字符串进行比较运算符的查询
mysql> select * from DemoTable1881 where
cast(substring(UserEducationGap, 1, instr(UserEducationGap, '-')-1) as signed) >= 4;
这将产生以下输出 –
+--------+------------------+
| UserId | UserEducationGap |
+--------+------------------+
| 101 | 5-9 |
| 103 | 4-8 |
| 104 | 7-12 |
+--------+------------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程