我的员工表有一个工作级别列,其中包含 1A、1B、1C、2A、2B 和 2C 等值。员工等级为 2C > 2B > 2A > 1C > 1B > 1A。
现在,如果我编写这样的查询:
SELECT *
FROM employee
WHERE work_level >= 1C
它会返回仅包含工作级别 1C、2A、2B、2C 的行吗?
如果没有,那我该怎么办?
是的,它将返回所需的行,但请记住在 where 子句中添加一个逗号:
SELECT * FROM employee WHERE work_level >= '1C'
尝试这样的事情:
假设您有一些work_level
s 和 input 定义如下:
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 |
+---------------+---------------+--------------+---------------+