Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql query

Posted on 2011-09-08
4
Medium Priority
?
271 Views
Last Modified: 2012-05-12
I have two fields in a table (lets fieldone and fieldtwo) and trying to return results that are more accurate

I am collecting the search term from the user in a $_GET['q'].



Lets say the user types in "Find how to add a form"

I want to only return results where ONE of the words exists in EACH FIELD in the table.

Example below would return rows 1. and 3. only

      FIELDONE                       FIELDTWO
1.  add insert enter             new form
2.   delete                           form
3.   adding                          the form
3.   finding                          the form

0
Comment
Question by:debbieau1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36507594
How are you currently doing this? Are you using PHP to explode the passed string to an array (list of search words) or are you passing them directly to MySQL as one string. Are you using full-text searches on the MySQL side?
0
 
LVL 1

Author Comment

by:debbieau1
ID: 36507606
At the moment, I just have an sql query that is filtered by the $_GET

I can use full text searching but I am not that familiar with it.  My understanding is that I setup the field indexes for full text search.

I am interested in using the most efficient approach, as I anticipate a lot of searches being done.

 I would welcome your advice.
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36507665
Try this out. Use DESCRIBE or EXPLAIN and see if it will perform well in your environment.

SET @find = 'Find how to add a form';

SELECT *
FROM (
   SELECT 'add insert enter' FIELDONE, 'new form' FIELDTWO
   UNION SELECT 'delete', 'form'
   UNION SELECT 'adding', 'the form'
   UNION SELECT 'finding', 'the form'
) derived
WHERE FIELDONE REGEXP CONCAT('((', REPLACE(@find, ' ', ')|('), '))')
AND FIELDTWO REGEXP CONCAT('((', REPLACE(@find, ' ', ')|('), '))')
;

Open in new window


The above is a test script that will run directly as coded above in MySQL Workbench.

For the actual code, you can put this into PHP like:

$sql = "SELECT {column list}
FROM {your table name}
WHERE {column one} REGEXP CONCAT('((', REPLACE(".$find_str.", ' ', ')|('), '))')
AND {column two} REGEXP CONCAT('((', REPLACE(".$find_str.", ' ', ')|('), '))')
;"

Open in new window


You can probably optimize this by doing the string manipulation on the PHP side, but this was what popped into my head. What I am doing is taking your string and converting it to a regular expression like: '((word1)|(word2)|(word3))' so basically I made each word a matching group. The key to ensuring both fields match is the AND logical operator.
0
 
LVL 1

Author Closing Comment

by:debbieau1
ID: 36507856
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question