I am trying to understand what is the best way to construct a query string with php when there are multiple columns I want it to look for.
For example the database has id, name, email, date.
And I have a search input field on a page which when submitted, I want it to search based on the input field against the above mentioned columns. Best what is the best way/practice to do that?
I have the following so far but it seems like it is a “dumb” search.
"SELECT * FROM Table WHERE id LIKE '$search%' || name LIKE '$search%' || email LIKE '$search%' || date LIKE '$search%'";
Well this sort of works but I feel there has to be a better way and a more appropriate smarter method.
Thanks…
You cannot make mysql use index properly when you are querying multiple columns and not using AND (you are using OR)
There is always need to optimize your query strategy, always people think the data is small,
why bother to spend effort to optimize … but the truth is you don’t know when your data will grow
For your query, if you want to stick to LIKE,
then you need to build 4 indexes on the 4 columns,
And change the query to :-
What is Union ?
Off-topic issue :-