mysql match string with start of string in table

后端 未结 2 479
梦毁少年i
梦毁少年i 2021-01-07 19:34

I realise that it would be a lot easier if I could modify the table when it was created, but assuming I can\'t, I have a table that is such as:

abcd
abde
abd         


        
相关标签:
2条回答
  • 2021-01-07 20:13

    Try this:

    SELECT col1, col2 -- etc...
    FROM your_table
    WHERE 'abffagpokejfkjs' LIKE CONCAT(value, '%')
    

    Note that this will not use an index effectively so it will be slow if you have a lot of records.

    Also note that some characters in value (e.g. %) may be interpreted by LIKE as having a special meaning, which may undesirable.

    0 讨论(0)
  • 2021-01-07 20:17

    LIKE can be avoided, by truncating the comparison string to each value's length:

    ... WHERE LEFT('abffagpokejfkjs', LENGTH(value)) = value
    
    0 讨论(0)
提交回复
热议问题