How to search double quotes in fulltext search in sql?

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

Was this solution helpful to you?

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

Just Added Q & A:

Find solution

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.