Search code examples
mysqldatabasesql-like

MySQL combine columns before matching it with LIKE


I have two columns in my database that I want to combine before matching them using LIKE statement.

My table:

|---------------------------------|
|   ID     |   PREFIX  |  SUFFIX  |
|---------------------------------|
|   1      |     31    |   523    |
|---------------------------------|
|   2      |     62    |   364    |
|---------------------------------|

I want to be able to supply 315 and ID 1 would be returned. Is there any easy way of doing it? At the moment I am splitting search string and matching separate columns.

Thanks.


Solution

  • SELECT * FROM table WHERE CONCAT(PREFIX, SUFFIX) LIKE '%315%'