473,325 Members | 2,872 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,325 software developers and data experts.

Mysql LIKE Search query - How to get most accurate row first.

rahulephp
On my search result page, I wanted to search for "Nintendo DS Lite - Pink"

I used following code:

Ex:
Expand|Select|Wrap|Line Numbers
  1. $search_text = "Nintendo DS Lite Pink";
  2.  
  3. $kt=split(" ",$search_text);//Breaking the string to array of words
  4.  
  5. // Now let us generate the sql 
  6. while(list($key,$val)=each($kt))
  7. {
  8.     if($val<>" " and strlen($val) > 0)
  9.     {
  10.         $q .= " name like '%$val%' or ";
  11.     }
  12. }// end of while
  13.  
  14. //Remove the last 'OR'
  15. $q=substr($q,0,(strlen($q)-3));
  16.  


Than the $q would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `products`
  2. WHERE  
  3. name like '%Nintendo%' or  
  4. name like '%DS%' or  
  5. name like '%Lite%' or  
  6. name like '%Pink%'
  7.  

And i am getting Mysql Output given below:


1) Activity Meter - DS.
2) Nintendo DS Red.
3) Nintendo DS Lite Pink.
4) Nintendo DS Lite Turquoise.

But the third result is most accurate/relevant then first two result.

Please help me out to get the most accurate row first then the relevant rows as per their relevancy with search term "$search_text"

Many Thanks in Advance.
Aug 21 '10 #1
1 2776
Atli
5,058 Expert 4TB
Hey.

Try using a Fulltext search. In Natrual Language mode you can have it return a score for each match and sort them based on that. The downside to this method is that you can only use them with MyISAM tables, and you will have to slightly alter your tables.

You might find this article at the Zend Developer Zone helpful: Using MySQL Full-text Searching.
Aug 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Benjamin Dickgießer | last post by:
Hi, I want to create a sql query but don't know if this is possible with mysql. The Query should do the following: Select all db entries from table in which entry a is smaller than the number...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
1
by: WindAndWaves | last post by:
Hi Gurus Is there a way to search for a word in any of the fields of a row using simple sql for my sql? I have a rather complex search function on my site and right now, I dump all the text of...
13
by: Howard Kaikow | last post by:
In VB, I've developed the habit of using the API QueryPerformanceCounter to achieve the most accurate timings. In VB .NET, one can use TotalMilliseconds and Ticks to convert time to...
1
by: SC | last post by:
I'm developing a site that may eventually have a very large database of users (hopefully, but who knows). It will be a community website where users can search each other (think Friendster,...
2
by: turbosatan | last post by:
i need to send a query from a web app to a search page on my site i understand that to use googles search i would use something along the lines of http://www.google.com/search?=q but the...
1
by: vHTML | last post by:
hello everyone :) I am trying to make a page which would take a "entered" on search query from the previous page and display results in google with that query in an <iframe> in the results.htm...
2
by: fusonts | last post by:
Can someon show me how to wirte a mySQL search query to search from only the open records. This is what I have but it continues to return records that are closed so I'm obviously doing something...
6
by: paankhate | last post by:
Hi, I have a task at hand to reduce the time taken for search query to execute. The query fetches records which will have to sorted by degrees away from the logged in user. I have a function...
4
by: ziycon | last post by:
I'm have a search option on my site, i'm able to search one DB table no problem and display the results, my problem is i need to search multiple tables by this single 'search term' entered by the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.