Search code examples
mysqlstringsql-like

MySQL using like to match specific string


In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+" I simply need to get the column where I have multiples data and not only one.

I tried with

mycol NOT LIKE '%+'

But it didn't work...

Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'

If I had in my columns

'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'

I want to select only

'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+', 

NOT 'dvgff+' !


Solution

  • if you want to search '..xxx+..' then you should be use xxx+%

    if you want to search '..+xxx..' then you should be use %+xxx

    if you want to search '..++..' then you should be use %++%

    if you want to search '..+..+..' then you should be use %+%+%


    It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...

    so you can try like '%+%+%' to exclude just one '+'

    CREATE TABLE TestTable
        (`text` varchar(90))
    ;
    
    INSERT INTO TestTable
        (`text`)
    VALUES
        ('jdsjpgsg+jdsjpgsg+'),
        ('dvgff+'),
        ('ffef+eefds+ghghgh+')
    ;
    

    select * from TestTable
    where text like '%+%+%'
    

    |               text |
    |--------------------|
    | jdsjpgsg+jdsjpgsg+ |
    | ffef+eefds+ghghgh+ |
    

    SQL Fiddle Demo Link