SQL: search UNION question?
-
I have a sql query that I use for search: $searchQuery = "John"; // example input $getinfo = mysql_query(" SELECT `nickname`, `description`, `moreinfo` FROM `users` WHERE `nickname` LIKE %$searchQuery% UNION SELECT `nickname`, `description`, `moreinfo` FROM `users` WHERE `description` LIKE %$searchQuery% UNION SELECT `nickname`, `description`, `moreinfo` FROM `users` WHERE `moreInfo` LIKE %$searchQuery% ORDER BY `nickname` ASC LIMIT 15 "); My question is: This is the correct way? or there are more efficient way to do it and get the same results? The code above is faster enough to echo 15 results, but there is another way to do this without wasting ram or the server processing? then I use a while echoing the $xResult["nickname/dec and moreinfo..."];
-
Answer:
You can combine this into 1 SELECT by using OR in your WHERE clause: SELECT `nickname`, `description`, `moreinfo` FROM `users` WHERE `nickname` LIKE %$searchQuery% OR `description` LIKE %$searchQuery% OR `moreInfo` LIKE %$searchQuery% ORDER BY `nickname` ASC LIMIT 15 I would guess that would be slightly faster, although probably not by a huge amount, unless the table is very large. The LIKE query is probably the more expensive operation, and combining them using OR doesn't change that. But give it a try and see if there is any real difference.
Ratchetr at Yahoo! Answers Visit the source
Other answers
You can combine this into 1 SELECT by using OR in your WHERE clause: SELECT `nickname`, `description`, `moreinfo` FROM `users` WHERE `nickname` LIKE %$searchQuery% OR `description` LIKE %$searchQuery% OR `moreInfo` LIKE %$searchQuery% ORDER BY `nickname` ASC LIMIT 15 I would guess that would be slightly faster, although probably not by a huge amount, unless the table is very large. The LIKE query is probably the more expensive operation, and combining them using OR doesn't change that. But give it a try and see if there is any real difference.
Ratchetr
Related Q & A:
- How To Send Money With Western Union?Best solution by Yahoo! Answers
- What if union of disjoint sets results in universal set?Best solution by Mathematics
- How can I optimize this dynamic SQL query in oracle with PL/SQL?Best solution by docs.oracle.com
- Who is the best rugby union player of all time?Best solution by Yahoo! Answers
- Does any one know how to join the Operating engineers local union #3?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.