Search code examples
phpmysqlcodeigniter-2

How to use comparison operator for numeric string in MySQL?


I have an employee table which has field like experience with VARCHAR type, this field combines both total year and total month separated by dash (-), so I have to filter experience by year who has more than 3 years experience.

My table structure:

enter image description here

So now I have to get more than 3 years experienced id's. I tried like below,

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

I know comparison operator will not support for string but I do not have any new solution for that. Is there any solution?


Solution

  • better answer based on converting to number:

    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 would probably be better for readability. wouldn't likely have any affect on the query.