Starting off, I apologize for bad table structure, it was not my decision and existed before me.
Anyway, I have a table tbl_cities that is a list of, you guessed it, cities (but for whatever reason are stored in column [desc]). I wanted to make a nifty AJAX text input that, as you type, it tries to find out what cities you are typing and offer them as suggestions. It's kinda like this example. So my query looked like this
SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'
Which works fine. However, I see there are a bunch of misspellings in this table, so I want to add fuzzy logic. I want to keep getting cities that match the first letters they type, so I have this query.
SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'
OR DIFFERENCE('phil', [desc])>3
Now I want to sort based on the [desc] LIKE 'phil%' before the fuzzy logic part. So in this example, Philadelphia should appear before random cities like PAOLA and POWELL
I would try something like that :
order by case when [desc] like 'phil%' then 0 else 1 end, [desc]
Related
SELECT * FROM store WHERE
concat_ws(name, type, location) LIKE :search1 OR :search2 OR :search3
for($n=0; $n<$count; $n++)
$query->bindValue(':search'.$n, '%'.$search[$n].'%', PDO::PARAM_STR);}
}
I have a search query, i break user's input into array, ex:array('i', 'love', 'apple');
my question is how to ORDER by the closest match?
it search 3 columns, so if user type new york apple, it will return many content with new york from location column, and many content are nothing to do with apple
If you want to do this, you will need Full Text Search(http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html). Which is a bit more complicated than LIKE, but certainly quicker and smarter.
You can do this without full text search (where the default values wouldn't work anyway for your query because "i" wouldn't be treated as a word):
order by ((concat_ws(name, type, location) LIKE :search1) +
(concat_ws(name, type, location) LIKE :search2) +
(concat_ws(name, type, location) LIKE :search3)
) desc
This orders by the most matches to the fewest. Your where clause needs to follow the same format as well, with three different like clauses.
In a query in which I am looking for results based on the title of articles, I use the LIKE part as followed:
WHERE title LIKE %searchquery%
In my database one title is like this:
Economy in America
My problem:
With my current query, this title does NOT get listed, when the user enters the following searchquery:
america economy
When the user enters only one of these terms, everything works fine and the title gets listed.
How come?
How do I need to adjust my query so that my sql query will also work when the user enters more than one term?
The MySQL LIKE operator is limited in that it doesn't offer full regex support of the sort for which you are really looking. That being said, one option you could try would be to split your search query terms and then join multiple LIKE conditions using AND. Consider this example:
WHERE title LIKE %queryterm1% AND LIKE %queryterm2%
If queryterm1 be 'america' and queryterm2 be 'economy', then this would match the title 'Economy in America'.
You will need to split the query by whitespaces and then add multiple LIKE statements like this:
WHERE title LIKE '%america%' AND title LIKE '%economy%'
As #Tim says, you must write some routine in your application to divide the string terms and inject them in the select where clause. Or you may need to write some stored procedure for that.
I want to filter according to selection but what I'm trying to do is that if Q1 and Q2 option selected I want to filter them for both Q1 and Q2. Here is code I wrote and not working:
Like IIf([Forms]![Form1]![Combo1]="All";"Q1" Or "Q2";[Forms]![Form1]![Combo1])
After running this code, program changes "Q1" Or "Q2" part to ([QQuery].[Q])="Q1" Or ([Q_Query].[Q])="Q2".
I'm already able to filter one by one, I mean for Q1, for Q2 or for Q3 seperately. How can I filter, for Q1 and Q2 at the same time. Thanks.
OK, I finally did it! This what I wrote:
IIf([Forms]![Form1]![Combo1]="Q1+Q2";"Q1"; [Forms]![Form1]![Combo1]) Or IIf([Forms]![Form1]![Combo1]="Q1+Q2";"Q2";[Forms]![Form1]![Combo1])
and so on. but if you want to add All part, you have to add to the end of codes above:
Or Like IIf([Forms]![Form1]![Combo1]="All";"*";[Forms]![Form1]![Combo1])
Hope this helps other people having same problem Thanks again Linger :)
You are not specifying a wild card so you are not actually using LIKE and the result would be the same as if you used equality operator (=).
I do believe the below will work better for you. Replace Quarter with the actual field name. I put them in parentheses just in case you have any other criteria you are adding to the WHERE clause.
IIf([Forms]![Form1]![Combo1]="All"; "(Quarter = 'Q1' Or Quarter = 'Q2')";[Forms]![Form1]![Combo1])
If you truly need to use LIKE then it should look something like the following:
IIf([Forms]![Form1]![Combo1]="All";"(Quarter LIKE '*Q1*' Or Quarter LIKE '*Q2*')";[Forms]![Form1]![Combo1])
I am currently working on a search feature for a website that searches through a database for a specific animal.
Say the user inputs rabbit, the search will go through the db and display the results for rabbit.
Now say a user inputs bunny the search will go through the db but will not find a match for bunny.
Most people know that bunny means rabbit, but the database doesn't know that. At this point I have implemented a MySQL thesaurus within the same database to search for synonyms of what the user inputs.
This means that if the user inputs bunny it will display a list of synonyms for bunny.
In that list there is the word Rabbit and I am trying to pull that word out of there to generate a match. At this point I have the following.
"SELECT `engname` FROM `searchtestdb` WHERE `engname` IS NOT NULL ";
-- This displays the english name of every animal within that table. --
"SELECT synonyms.* FROM words LEFT JOIN synonyms ON synonyms.word_id = words.word_id WHERE word = \"$searchBox\""
-- This displays the synonyms for $searchBox which is the word the user inputs. --
Both of these queries display what I want them to display. In other words, the first query gives me all of the animals names in the table, and the second query gives me the synonyms for the word the user inputed.
At this point my problem is how to compare the synonyms to all the animals names. I've tried several queries with the LIKE command but I keep getting syntax errors.
Is what I am asking possible? If not what would be a better course of action? Any help would be greatly appreciated.
Thank You.
I got a semi fiddle going for y'all.
http://sqlfiddle.com/#!2/47d42/3
It only works for "bunny" since the entire synonym and word list is too big for fiddle.
select * from searchtestdb
where engname in
(
SELECT synonyms.synonym
FROM words
LEFT JOIN synonyms ON synonyms.word_id = words.word_id
WHERE word = "bunny"
)
SQLFIddle
EDIT: Since you probably also want to search for word directly inputted and not just it's synonyms, you should also add that condition:
OR engname = "bunny"
SQLFIddle
I think the idea is this: (pseudocoded)
Create a function which returns true or false (with a parameter as the search word) of whether there exists a result. This is a basic
SELECT COUNT > 0 FROM table WHERE text LIKE %parameter%
Now create a function that returns a table of results:
loop through the the synonyms of the word which again comes as a
parameter to this function
add to table of the results if a synonym in a loop fits the function above.
return the table
People have different ideas of how to search for the same term.
For example Tri-Valley, Trivalley, Tri Valley (and possibly even incorrect spellings)
Currently that search is done like this
SELECT * FROM `table` WHERE `SchoolDistrict` LIKE '%tri valley%';
Is there an easy way to say 'space dash or no space' without writing out three like statements?
It seems like it could easily be done:
SELECT * FROM `table` WHERE `SchoolDistrict` LIKE '%tri%valley%';
But this only works if the initial input is 'tri-valley' or 'tri valley' If the initial input is 'trivalley' I have no idea where to place the % (theoretically that is, actually, I do, as we are only looking at about a dozen different school districts, but I'm looking to solve the larger problem)
You could consider using SOUNDEX, or SOUNDS LIKE if you have a lot of incorrect spellings. If you've got a lot of rows (or even if you don't), it might be wise to store the output of the SOUNDEX in an additional column.
I'd also recommend -- in the interests of accuracy -- introducing a separate table with an authoritative list of school districts, and run a query to find those which aren't in that list.
MySQL has a function called Sounds like.
link text
An alternative here is to recast the problem from search to select, if possible. Instead of letting your users enter free-form text to choose a school district, if you have a set of school districts generate a dropdown (or set of cascading dropdowns if the list is large, say by county, then by school district) and allow the user to select the appropriate one. Use this both for "searching" and for data entry to eliminate non-canonical entries. Obviously this only works when you can enumerate all of the entries.
Alternatively you could allow the user to choose a starts with or contains type search and simply generate the appropriate SQL ('tri%' or '%tri%') based on the selected search type. If the user understands that the search type is starts with or contains, they will likely adjust their search string until it yields the results they need.
The second statement you posted should do the trick:
SELECT * FROM 'table' WHERE 'SchoolDistrict' LIKE '%tri%valley%';
What you should do before you pass the search term into the select statement is to replace all characters and spaces with the % sign. For example,
SearchTerm = SearchTerm.Replace(" ","%");