Solved

sql query

Posted on 2011-09-08
4
265 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now