如何在mysql中使用数字字符串的比较运算符?

时间:2015-12-25 04:12:03

标签: php mysql codeigniter-2

我有一个员工表,其中有类似varchar类型的经验,该字段结合了总计年份和总月份,以短划线( - )分隔,因此我必须按年度过滤具有3年以上经验的经验。< / p>

我的表结构:

enter image description here

所以现在我必须经历超过3年的经历。 我尝试过如下,

SELECT * FROM employee WHERE experience LIKE '>=3%';

我知道比较运算符不支持字符串,但我没有任何新的解决方案,有没有解决方案?

3 个答案:

答案 0 :(得分:1)

基于转换为数字的更好答案:

select * from employee where cast(substring(experience, 1, instr(experience, '-')-1) as signed) >= 3;

mysql> select cast(substring('11-3', 1, instr('11-3', '-')-1) as signed);
+------------------------------------------------------------+
| cast(substring('11-3', 1, instr('11-3', '-')-1) as signed) |
+------------------------------------------------------------+
|                                                         11 |
+------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('11-3', 1, instr('11-3', '-')-1) as signed) > 3;
+----------------------------------------------------------------+
| cast(substring('11-3', 1, instr('11-3', '-')-1) as signed) > 3 |
+----------------------------------------------------------------+
|                                                              1 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('11-3', 1, instr('11-3', '-')-1) as signed) >= 3;
+-----------------------------------------------------------------+
| cast(substring('11-3', 1, instr('11-3', '-')-1) as signed) >= 3 |
+-----------------------------------------------------------------+
|                                                               1 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('3-0', 1, instr('3-0', '-')-1) as signed) >= 3;
+---------------------------------------------------------------+
| cast(substring('3-0', 1, instr('3-0', '-')-1) as signed) >= 3 |
+---------------------------------------------------------------+
|                                                             1 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('2-11', 1, instr('2-11', '-')-1) as signed) >= 3;
+-----------------------------------------------------------------+
| cast(substring('2-11', 1, instr('2-11', '-')-1) as signed) >= 3 |
+-----------------------------------------------------------------+
|                                                               0 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('22-11', 1, instr('22-11', '-')-1) as signed) >= 3; 
+-------------------------------------------------------------------+
| cast(substring('22-11', 1, instr('22-11', '-')-1) as signed) >= 3 |
+-------------------------------------------------------------------+
|                                                                 1 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select cast(substring('11-0', 1, instr('11-0', '-')-1) as signed) >= 3; 
+-----------------------------------------------------------------+
| cast(substring('11-0', 1, instr('11-0', '-')-1) as signed) >= 3 |
+-----------------------------------------------------------------+
|                                                               1 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

unsigned可能对可读性更好。不会对查询产生任何影响。

答案 1 :(得分:1)

Select * from main where cast(split_str(experience, '-', 1) as unsigned) >= 3;

split_str(它是一个用户函数:http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/,默认情况下不是在mysql中)会用你的分隔符拆分你的列,而cast会将你的字符串变成一个无符号的int,然后你可以测试它。 另外,如果有一天你要测试几个月和几年,比如3年零10个月以上的所有经验,你可以用十进制转换你的值(这也适用于上面的测试):

Select * from main where cast(replace(experience, '-', '.') As decimal(5,2)) > 3.10;

我可能做过一些拼写错误(在我的智能手机上打字),但你已经明白了。

答案 2 :(得分:0)

这个简单案例的strcmp怎么样?如果您需要年份和月份,它将无法工作。

mysql> select strcmp('3-','3-1');
+--------------------+
| strcmp('3-','3-1') |
+--------------------+
|                 -1 |
+--------------------+
1 row in set (0.00 sec)

mysql> select strcmp('3-','3-9');
+--------------------+
| strcmp('3-','3-9') |
+--------------------+
|                 -1 |
+--------------------+
1 row in set (0.00 sec)

mysql> select strcmp('3-','2-9');
+--------------------+
| strcmp('3-','2-9') |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

你在这样的查询中使用它:

mysql> select * from main where strcmp('ab', url) = 1;
+----+---------------+
| id | url           |
+----+---------------+
|  1 | a.com         |
|  2 | aa.com        |
|  3 | aaa.com       |
|  4 | aachen.com    |
|  5 | aah.com       |
|  6 | aaliyah.com   |
|  7 | aaliyahs.com  |
|  8 | aardvark.com  |
|  9 | aardvarks.com |
| 10 | aaron.com     |
| 11 | aas.com       |
+----+---------------+
11 rows in set (0.02 sec)

因此,在您的情况下,您需要select * from employee where strcmp('3-', experience) = -1;

之类的内容

如果我是数据库管理员,如果提到的experience意味着这个工作多年,我希望我刚刚使用时间戳开始和结束,因此可以很容易地计算出来。否则,每个月批处理作业都必须更新该字段。

请参阅http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#function_strcmp

[更新:通过检查字符串的年份部分是否大于一位数来修复11至29的情况]

select * from employee where strcmp('3-', experience) = -1 or instr(experience, '-') > 2;