Solved

sql query

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

733 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