Search code examples
mysqlmultiple-columnssql-like

How to search LIKE string value in multiple columns?


I've read multiple solutions to find if one value is in multiple columns, but I want to see if the (string) value is like the value in one or more columns.

So basically said can I do a LIKE OR on multiple columns in one elegant statement?

A better way to do this:

    SELECT * FROM table WHERE col0 LIKE '%value%' OR col1 LIKE '%value%' OR col2 LIKE '%value%' OR ...;

And this then for the rest 10 or so columns.

KR


Solution

  • This (python) bit gives me the results I want.

        columns = ["col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8"]
        sql = "SELECT * FROM table WHERE col0 LIKE '%"+_name+"%'"
        for column in columns:
            sql += "OR "+column+" LIKE '%"+_name+"%s'"
        sql += ";"
    

    There must be a better way but this works for now.