Solved

sql query

Posted on 2011-09-08
4
266 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
  • 2
  • 2
4 Comments
 
LVL 59

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 59

Accepted Solution

by:
Kevin Cross earned 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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