473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

multi-word query

Hello all, I'm trying to figure out how to search a "key_words" column,
that contain key words (obviously) for that row. An example of what one
of the key_words contains is: "home interior decorating". I can search
for "home interior" or "interior decorating" and it will return the
correct row, but if I search for "interior home" or "home decorating" I
get nothing. I need it to return any row with ALL of those words in any
order. I am using WHERE LIKE '%search%'. I have done some searching,
but was never quite sure what to really search for. Is there an easy
way around this or can someone point me in the right direction? THANKS!

Aug 1 '05 #1
14 1737

"kiqyou_vf" <so*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello all, I'm trying to figure out how to search a "key_words" column,
that contain key words (obviously) for that row. An example of what one
of the key_words contains is: "home interior decorating". I can search
for "home interior" or "interior decorating" and it will return the
correct row, but if I search for "interior home" or "home decorating" I
get nothing. I need it to return any row with ALL of those words in any
order. I am using WHERE LIKE '%search%'. I have done some searching,
but was never quite sure what to really search for. Is there an easy
way around this or can someone point me in the right direction? THANKS!


WHERE key_words like %home% AND key_words like %interior% AND key_words like
%decorating%
Aug 1 '05 #2
One option would be something like this:

If you're searching for (foo) and (bar)

select *
from my_table
where key_words like '%foo%'
and key_words like '%bar%'

Aug 1 '05 #3
I'm sorry, I forgot to mention that the '%search%' is coming from a
form- $_GET['search']

Aug 1 '05 #4
kiqyou_vf wrote:
I'm sorry, I forgot to mention that the '%search%' is coming from a
form- $_GET['search']


You may want to use the explode() function to split the $_GET text into
and array of 'words' first. Then loop through this array of words adding
the '%' chars to the beginning and end of each word while building the
sql statement.

Carl.
Aug 1 '05 #5
On 2005-08-01, kiqyou_vf <so*******@gmail.com> wrote:
Hello all, I'm trying to figure out how to search a "key_words" column,
that contain key words (obviously) for that row. An example of what one
of the key_words contains is: "home interior decorating". I can search
for "home interior" or "interior decorating" and it will return the
correct row, but if I search for "interior home" or "home decorating" I
get nothing. I need it to return any row with ALL of those words in any
order. I am using WHERE LIKE '%search%'. I have done some searching,
but was never quite sure what to really search for. Is there an easy
way around this or can someone point me in the right direction? THANKS!


Have a look at http://dev.mysql.com/doc/mysql/en/fulltext-search.html
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Aug 1 '05 #6
Damn, I was hoping that it wouldn't be the only way. I looked at that
earlier and couldnt understand anything. Are there any tutorials that
use fulltext search that you know of? I mean, if thats the way I have
to do it, then I'll do it but I was really hoping for a quicker
solution- or a work around. Anyway, thanks for pointing me in the
proper direction.

Aug 1 '05 #7
kiqyou_vf wrote:
Damn, I was hoping that it wouldn't be the only way. I looked at that
earlier and couldnt understand anything. Are there any tutorials that
use fulltext search that you know of? I mean, if thats the way I have
to do it, then I'll do it but I was really hoping for a quicker
solution- or a work around. Anyway, thanks for pointing me in the
proper direction.


Why wouldn't you just use multiple 'anded' tests when pattern matching?

"SELECT * FROM `table` WHERE `description` LIKE '%interior%' AND
`description` LIKE '%home%'"
Carl.
Aug 1 '05 #8
I'm sorry, I guess I don't understand.'%interior%' and '%home%' need to
come from a query entered into a text box, not a pre-defined link.

Is there a way I could put the multi-word search into an array,
seperated by spaces and make each item in the array print out " LIKE
'%item1+1%' " with a loop? Does that make sense?

Aug 2 '05 #9
$searchTerms = explode(' ', $_GET['search']);
$query = 'select * from my_table where ';

foreach($searchTerms as $term)
$query .= "key_word like '%" . $term . "%' and ";

$query = substr($query, 0, -4); //get rid of the last 'and'

Aug 2 '05 #10
kiqyou_vf wrote:
I'm sorry, I guess I don't understand.'%interior%' and '%home%' need to
come from a query entered into a text box, not a pre-defined link.

Is there a way I could put the multi-word search into an array,
seperated by spaces and make each item in the array print out " LIKE
'%item1+1%' " with a loop? Does that make sense?


I'm not sure I understand what you are asking here, but ill give it
another try based on my understaning of your first post.

If you search using a clause like "%home interior%", you will find
matches that have the phrase "home interior" in the field. If you search
for "'%home%' AND '%interior%' you will get matches that have the
_words_ 'home' and 'interior' in the field, though not necessarily in
that order.

for example:
Select * from table where field like '%home interior%'
matches "new home interior blah" but not "interior ideas for your home".

Select * from table where field like '%home%' and field like '%interior%'
will match both.

I hope that helps.
Carl.

Aug 2 '05 #11
Thats it Zeldor, I'm going to try that out.

Aug 2 '05 #12
Take a chance and try my code
<?
// GET search query
$where = trim($_GET['where']);

// some vars
$search_col_name = "col";
$select_collumns = "*";
$select_tables = "table1";

// remove all special chars
$where = ereg_replace("[^0-9a-zA-Z ]", "", $where);

// procced first var
$where = preg_replace("/^[ ]*(\w)+/", "%$1%", $where);
// replace all AND into SQL-like expressions
$where = preg_replace("/ and (\w)+/", "' AND $search_col_name LIKE
'%$1%", $where);
// replace all OR into SQL-like expressions
$where = preg_replace("/ or (\w)+/", "' OR $search_col_name LIKE
'%$1%", $where);
$where = "WHERE $search_col_name LIKE '".$where."';";
$sql_query = "SELECT $select_collumns FROM $select_tables ".$where;
echo $sql_query;
?>

Aug 2 '05 #13
what does "%$1%" do?

Aug 2 '05 #14
Try to google "perl regular expressions". I'm sure it'll be easy to find

Aug 2 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.