MySQL在alpha-numeric列上的条件

时间:2012-07-15 05:54:35

标签: php mysql

我的员工表有一个工作级别列,包含1A,1B,1C,2A,2B和&等值。 2C。 员工等级是2C> 2B> 2A> 1C> 1B> 1A。

现在如果我写这样的查询:

SELECT *
FROM employee
WHERE work_level >= 1C

它会返回仅包含工作级别1C,2A,2B,2C的行吗?

如果没有,那我该怎么办?

2 个答案:

答案 0 :(得分:1)

是的,它将返回所需的行,但请记住在where子句::

中添加一个倒置的逗号
SELECT * FROM employee WHERE work_level >= '1C'

答案 1 :(得分:0)

尝试这样的事情:

我们假设您有一些work_level和输入,如下所示:

set @work_level1 = '25C';  
set @input_work_level1 = '1C';  

set @work_level2 = '2C';  
set @input_work_level2 = '15C';  

set @work_level3 = '2A';  
set @input_work_level3 = '1C';  

set @work_level4 = '2C';  
set @input_work_level4 = '15B';  

运行以下查询:

select   
 ( cast( left(@work_level1, length(@work_level1)-1) as unsigned )
   >=
   cast( left(@input_work_level1, length(@input_work_level1)-1) as unsigned )
 )
 and
 right(@work_level1, 1) <= right(@input_work_level1, 1)
 as '''25C'' >= ''1C''',

 ( cast( left(@work_level2, length(@work_level2)-1) as unsigned )
   >=
   cast( left(@input_work_level2, length(@input_work_level2)-1) as unsigned )
 )
 and
 right(@work_level2, 1) <= right(@input_work_level2, 1)
 as '''2C'' >= ''15C''',

 ( cast( left(@work_level3, length(@work_level3)-1) as unsigned )
   >=
   cast( left(@input_work_level3, length(@input_work_level3)-1) as unsigned )
 )
 and
 right(@work_level3, 1) <= right(@input_work_level3, 1)
 as '''2A'' >= ''1C''',

 ( cast( left(@work_level4, length(@work_level4)-1) as unsigned )
   >=
   cast( left(@input_work_level4, length(@input_work_level4)-1) as unsigned )
 )
 and
 right(@work_level4, 1) <= right(@input_work_level4, 1)
 as '''2C'' >= ''15B'''
;

会导致:

+---------------+---------------+--------------+---------------+
| '25C' >= '1C' | '2C' >= '15C' | '2A' >= '1C' | '2C' >= '15B' |
+---------------+---------------+--------------+---------------+
|             1 |             0 |            1 |             0 |
+---------------+---------------+--------------+---------------+