Search code examples
mysqlsqlmysql-workbench

SQL WHERE statement multiple columns LIKE one value


Trying to get a SQL query where you define the value once and it searches multiple columns. I got this working for the equal operator, but I need this for a LIKE operator as well. This is were the problem is.

SELECT *
FROM table1
INNER JOIN table2 ON table2.id = table1.ref_id
WHERE (table1.email, table2.email) LIKE '%example@example.com%'

This is my current query which is not working. Looked at a few other questions online, but those solutions didn't work for me.

How can I search multiple columns for one value with a LIKE operator?


Solution

  • The specific answer to your question is something like this:

    WHERE CONCAT_WS('|', table1.email, table2.email) LIKE '%example@example.com%'
    

    That would generally not be used. The more common approach is simply:

    WHERE table1.email LIKE '%example@example.com%' OR
          table2.email LIKE '%example@example.com%'