In my MySQL DB I have a list of terms like this (With First letters Capital and most of the time plurals)
Hairdressers
Restaurants
Beauty Salons
Furnitures For Restaurants
On my website I have a search bar where the user can search for those word (my website is a kind of POI finder). The search bar has a auto-suggest function, but I'm having problems with my query.
SELECT * FROM TABLE WHERE word = 'userword%'
So if the user enter "Res" it will return all the word starting the "Res". Fair enough, that works, But in my DB i have also words with space between them (Furnitures For Restaurants) and I would like that if the user enter "Res" the mysql query ALSO return Furnitures For Restaurants. I have tried this
SELECT * FROM TABLE WHERE word = '%userword%'
Yes great this works, but... it is a bit to much relax.. it return all and everything, so if the user enters "a" it will return all the word having a in it... And I don't want this.
So how can make the query works only on the FIRST letters of each word ? Exactly like my first query but taking in consideration words with spaces?
Look into mysql FULL Text Search feature. Look at this article too, explains mysql full text searching in a very detailed way.
Related
I was actually doing somthing else and came accross this intresing W3schools tutorial/ section in relation to SQL databases (its a curiousity killed the cat thing more then anything)
The page link where my question comes from is as follows
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where
the example lists all records that include mexico in the countrys catagorie
I can for instance change this to spain and all entrys with spain are filterd through.
The question I have is, it seems very unlikely to me that an end user is going to type out the entire code
SELECT * FROM Customers
WHERE Country='Mexico';
everytime they want too search a city (this example thus being mexico). I'm presuming we can get the city name inserted from a textbox, but ive looked can cant fand any examples on how to do this?
Try this:
SELECT * FROM Customers where <Column> like '%MEXICO%';
'%' is the wildcard. So you will find:
" MEXICO"
"MEXICO City"
"MX MEXICO"
If you want to use the wildcard you must use 'like' instead of '='
I've got a custom searchable member page on a WordPress site running s2member and I have a client that wants to use the mutli select box feature for the "category" data listed members can enter. My current code falls down here as the REGEX I currently use to select the category from the meta_value field only works with the single select box.
Here is the data as it appears in the database field:
s:8:"category";a:1:{i:0;s:17:"Business services";}}
Here is my current AND line in my SQL statement:
AND UMS.meta_value REGEXP '.*\"".$_SESSION['searchfield']."\";s:[0-9]+:\".*".$_SESSION['searchvar'].".\".'
How can I modify the above REGEX to work with the new multi option data? I’ve tried a few things but REGEX isn't a strong point of mine.
Thanks in advance!!
First time implementing Full Text Search...
I've run the necessary ALTER TABLE SQL to enable FULLTEXT on the applicable tables/fields.
On the following beta site: http://wtc.betaforming.com/
If I do a search for "leadership", I get normal results except for the Events section at the bottom. I have Events with that word in the event title and throughout the description copy (http://wtc.betaforming.com/events/event/?event_id=10039).
If I do a search for "communications", I get results in the Events section, which makes me think I have everything configured correctly.
I'm using the following basic code for testing purposes:
SELECT *
FROM tblevents
WHERE MATCH(event_title, event_desc_long, event_desc_short, event_tab_one_title, event_tab_one_text, event_tab_two_title, event_tab_two_text, event_tab_three_title, event_tab_three_text, event_tab_four_title, event_tab_four_text) AGAINST ('$site_search_term')
This is the same code I'm using to search Products and Articles (changing the necessary FROM and WHERE information).
Not sure what is happening (since it works for some phrases) or where to start looking in my db to see what is wrong.
Thanks
Brett
The answer to my question can be found here:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
My search term was over the 50% threshold - you have to read all the way to the bottom to find this.
I've implemented the "IN BOOLEAN MODE" for my events search and everything appears to be working correctly. I also used this article for help:
http://devzone.zend.com/26/using-mysql-full-text-searching/
I have a database where a field (plate) has values like ABC123, ABB-123, ... Some users enter a - in between, some don't.
Now I'm building a search function where the user can perform a search, for example 'ABB123' which the search function transform to plate LIKE 'ABB123' but I would like that this search returns 'ABB-123' in this case.
Has someone any idea how to pull this off?
Note: database is InnoDB, so full-text search isn't possible. Since each user has his own database, I'm not planning to use a third-party search server either.
Can you search for both instances?
plate = 'ABB123' OR plate = 'ABB-123'
Or if the format of plate is rather consistent, could you do something like
plate LIKE 'ABB%123'
That would get both instances.
I've found boolean mode of MySQL full text search useful, however there are a couple of things I can't seem to figure out how to achieve.
For instance imagine I have a full text column containing the words "Steve's Javascript Tutorial - Part One".
I would like to match this for each of the following searches: "tutorials", "javascript tutorials", "java", "java script", "script"
Imagine that each of those searches is simply assigned to a variable in whatever language may be being used (I always use PHP).
How could I modify this to make sure that Steve's article is returned on each of those searches?
MATCH (article_title) AGAINST ('"+$variable+"*' IN BOOLEAN MODE)
This is impossible ;)
When you search for "tutorials" the entry will not be found, because the entry in the database is singular and the search term is plural. You should do some form of "word stemming" before inserting the values to the database (and on search).
When you have done this, your expression will work. For searrch terms with more words (spaces) you should add the asterix to every word.