I was trying to find the best way to search multiple columns for the occurence of a word. Ofcourse an option would be to add multiple OR
's in the query:
SELECT * FROM table WHERE column1 LIKE %term1% OR column2 LIKE %term1% OR......
Yet I though this made the query quite long, especially when there are multiple terms
In my search I came across the CONCAT
function in MYSQL. This seems to allow me to make one search after concatenating the columns (right?):
SELECT * FROM table WHERE CONCAT(column1,column2,....) LIKE %term1% OR CONCAT(column 1,column2,...) LIKE %term2% ....
This has the advance of being easier to read and maintain
Because I do not yet understand the full use of CONCAT
I do not know or this is a correct way to use it. I was wondering what is the correct and most efficient way to achieve this search. (note: I am also planning on having it sort as stated in this stackoverflow question: mysql SORT BY amount of unique word matches, maby there are problems while using a method with this?)
EDIT:
Maby it would be easier to just explain my final wish: In php i have an array of single words. I wish select all rows from my database where 1 of the words occurs in one of multiple colmuns and then sort them by relevance (or even add extra importance to some columns). (note that there are only a few thousands (less than 5000) rows to search)
OR LIKE
may use index, but that's not guaranteed, in common case it won't. Thus, bith ways are bad and the only thing matter may be readability. And that's your decision to make. – Alma Do Jul 22 at 14:24