WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-14-2004, 08:15 AM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default Search function mySQL problem

Hi all,
I have a problem that i hope you can help me with. I am developing a site for a training company http://www.pitmanskills.com. The problem is that there is a course search within the site that isnt working to plan. The courses are held in a mySQL database with the following fields;

Title, Aims and Objectives, Prerequsites, Course Content etc.

Originally i used an SQL statement to select from by courses by the course title, this wasnt sufficient as when searching for 'databases' our MSAccess courses werent found. Next i used all fields to get the information. This again isnt right because selecting by all these fields means the most appropriate course appears miles down the list returned.

i.e. when searhing for "word" the "word 2000 expert" course appear below several other non-relevent courses with the word "word" in on of their fields.

How do i select all the results from the following statement :-


select * from pitman_courses
where `Course_Title` like \"%$trimmed%\"
OR `Target_Audience` like \"%$trimmed%\"
OR `Prerequisites` like \"%$trimmed%\"
OR `Aims_and_objectives` like \"%$trimmed%\"
OR `Course_Content` like \"%$trimmed%\"
order by Course_Title


But then put the results returned by;

select * from pitman_courses
where `Course_Title` like \"%$trimmed%\"


at the top?

Thanks, Chris.
Reply With Quote
  #2 (permalink)  
Old 07-14-2004, 08:19 AM
HardCoded HardCoded is offline
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 453
HardCoded RepRank 0
Default

What version of MySQL are you using?
Reply With Quote
  #3 (permalink)  
Old 07-14-2004, 08:25 AM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

im not sure ill check with the hosts, 5 mins.
Reply With Quote
  #4 (permalink)  
Old 07-14-2004, 08:27 AM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

I cant get hold of them, why is the added functionality that may be useful for my situation in later versions?
Reply With Quote
  #5 (permalink)  
Old 07-14-2004, 10:53 AM
ronniethedodger's Avatar
ronniethedodger ronniethedodger is offline
WebProWorld 1,000+ Club
 

Join Date: Aug 2003
Location: Central US
Posts: 1,576
ronniethedodger RepRank 0
Default

Might I suggest something simpler? Add a keywords field which will contain the key terms from each field. Arrange the terms in the order of importance. This would (of course) be a judgement call of a human who enters the course data, but it is cheap and easy way of "indexing" your data.

Your query would be against the keyword field, and depending upon the postition of the word in that field would give you the relevancy (or approximation of it).

I am not sure of the exact SQL statement, but it would involve selecting the strpos() INTO an array variable that holds the position of the search term in the keyword field, then doing a sort on that array.
__________________
Ronnie T. Dodger
Web Design Forum-Web Design & Development News
Reply With Quote
  #6 (permalink)  
Old 07-14-2004, 11:34 AM
steve0 steve0 is offline
WebProWorld Pro
 

Join Date: May 2004
Location: Austin, TX
Posts: 199
steve0 RepRank 0
Default

Applicable solutions depend in part on the version of MySQL you are using.

Sub-queries may be an option if you are running one of the latest versions;

From a mysql prompt, type '\s' or 'SELECT version();'
to have it dump the relevant info to the screen.
__________________
Hardcore Programming Solutions and Coffee Drinker
Reply With Quote
  #7 (permalink)  
Old 07-14-2004, 12:12 PM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

Ive got it version 3.22 i think.

I dont really want to do the keywords field because we have hundreds of courses and would be an admin nightmare.
Reply With Quote
  #8 (permalink)  
Old 07-14-2004, 04:46 PM
steve0 steve0 is offline
WebProWorld Pro
 

Join Date: May 2004
Location: Austin, TX
Posts: 199
steve0 RepRank 0
Default

If I understand your question, you want the course titles with keywords to appear first...

This may be over simplistic, but why not just run 2 queries?

The first being
select * from pitman_courses where Course_Title like '%$trimmed%'
with the output stating ..
"The Following Courses matched your query:"

Then
select * from pitman_courses
where `Target_Audience` like \"%$trimmed%\"
OR `Prerequisites` like \"%$trimmed%\"
OR `Aims_and_objectives` like \"%$trimmed%\"
OR `Course_Content` like \"%$trimmed%\"
order by Course_Title

with the output stating ..
"<HR>The following courses may also be of interest:"

(You may want to have the second query omit any results where Course_Title like '%$trimmed%'
to avoid duplicates.)
__________________
Hardcore Programming Solutions and Coffee Drinker
Reply With Quote
  #9 (permalink)  
Old 07-14-2004, 10:55 PM
HardCoded HardCoded is offline
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 453
HardCoded RepRank 0
Default

Even simpler to use the IF() function:
Code:
select *,
IF(`Course_Title` like \"%$trimmed%\",0,1) AS in_title
from pitman_courses 
where `Course_Title` like \"%$trimmed%\" 
OR `Target_Audience` like \"%$trimmed%\" 
OR `Prerequisites` like \"%$trimmed%\" 
OR `Aims_and_objectives` like \"%$trimmed%\" 
OR `Course_Content` like \"%$trimmed%\" 
order by in_title, Course_Title
Reply With Quote
  #10 (permalink)  
Old 07-15-2004, 11:42 AM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

Hi HardCoded,
I take my hat off to you, that was exactly what i was looking for. You are an SQL master.
Reply With Quote
  #11 (permalink)  
Old 07-15-2004, 12:33 PM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

Hi,
Spoke too soon, now if i type in 'Databases' i dont get ' Access 2000', now the key word is in the course content field but not in the title. I still need to recieve data that doesnt have the keyword in the title. But i want the ones with the keyword in the title to be ranked highest, understand?
Reply With Quote
  #12 (permalink)  
Old 07-15-2004, 03:56 PM
HardCoded HardCoded is offline
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 453
HardCoded RepRank 0
Default

The new query should return the exact same results as the old one, just ordered differently. Double check your results. Did you cut n' paste exactly as I had it, or did you have to alter it?
Reply With Quote
  #13 (permalink)  
Old 07-16-2004, 05:59 AM
icb01co2 icb01co2 is offline
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

Yup, my mistake, you actually are a star! :-) Thanks.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: function, mysql, problem, search



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Search Engine Friendly URLs by vBSEO 3.0.0