Solved

sql query

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

760 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

21 Experts available now in Live!

Get 1:1 Help Now