Search code examples
mysqlsql-likebetween

MySQL : between value in string


I'm trying to build a query which matches the rows following these rules :

  • my rows contents ids like 'MATCH_1', 'MATCH_2', 'MATCH_4'...
  • I want to match the rows which id is between 2 boundaries: SELECT id FROM table WHERE id LIKE "MATCH_%", % must be between 2 and 5 for example. The result must be : 'MATCH_2', 'MATCH_4', 'MATCH_5'

Is it possible to do so ?

Thanks


Solution

  • you mean this?

     SELECT * 
       FROM table
      WHERE row 
    BETWEEN 'MATCH_2' AND 'MATCH_5';
    

    or converted to int

      SELECT *
        FROM table
       WHERE CAST(SUBSTRING(row FROM 7) AS UNSIGNED)
     BETWEEN 2 AND 5;