Text matching in sql query (LIKE) - php

My Database:
ID place
-----------------------------------------------
1 Bhowali
2 Bangalore
3 Tumkur
My Query:
SELECT *
FROM table
WHERE place LIKE '%bhovali'
Question is: many users search wrong keyword on that time the query result should match and output correct one.. in my above query bhovali is a wrong word the correct word is bhowali. is there any way to get correct result from the query??..

may be this can help SELECT * FROM table WHERE SOUNDEX(name) LIKE SOUNDEX('bhovali') ;

This is a really loaded question... short answer is no. SQL is made for giving you what you want, not what you might want.
You would have to develop some kind of intelligence engine to recognize that there are similar names and search for those as it occurs.

in mysql you can use full text search if the column has the full text index as
select * from table match(name) against ('bhovali');

Related

How to match Value from string in Mysql PHP

I have a table methodology in mysql where a column contain values like 1,2,3,4,5,6 or 3,4,6 or 25,4,7,8 in multiple row.
Now in my scenario i have a company which contain id 6 and i want to Match this value with methodology table value. But i don't have any idea how i can do it.
I am trying that first it get all values from methodology table one by one and after that match value of company.
can anyone please help me??
You can use FIND_IN_SET(your_id, columnname) function.
Query will be like
SELECT * FROM
methodology
where FIND_IN_SET(your_id, columnname)
For more details about function please refer : http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
Use LIKE from mysql queries.
An example :
SELECT row FROM my_table WHERE my_column LIKE %6%
You need to use find_in_set function
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
To elaborate on #matthiasbe's incomplete answer, if you want to use LIKE, you should do as follows :
SELECT * FROM methodology WHERE companies LIKE '%,6,%' OR companies LIKE '6,%' OR companies LIKE '%,6'
This will match all rows where companies either has ,6, in its value or begins with 6, or ends with ,6.
This is far from being a perfect solution (both in clarity of the code and execution time), I'm just listing the options, hope it helps.

sql statement syntax for search

I would like to write an sql statement for search. Here is sample database structure.
For eg, I want to display all records with topic '13'. How can i write sql query for searching 13 from the above structure? Any suggestions?
Can i able to use WHERE Topic LIKE '%13%'? Anything wrong with this?
Try this one:
SELECT * FROM `TABLE_NAME` WHERE `Topic` LIKE "%13%";
It's better and faster to save it in a third table of many-to-many relationship.
If you want to save as per your example (single table), try to save data as eg ",10,13,15,"
always have coma before and after, thus the following sql will exclude 213 and 132 etc
select * from table_name where Topic like '%,13,%'
select * from table where find_in_set("13",topic);
or if topic is not used as a set, you could do ...
select * from table where concat(",",topic) like "%,13,%";
The 2nd isn't real elegant but I've had to do that a couple times.
Because the data isn't really normalized, I used concat to add a comma to the topic field so I could make sure the "like" comparison would pass with a comma before and after the value. I suppose we would also have to remove any unwanted spaces as well for this example, so ultimately it would end up like:
select * from TABLE where concat(",",replace(topic," ","")) like "%,13,%";
Ultimately, we have to know what to expect in the topic column to come up with a query that would always work. In the past, I've had situations where I would add values to a string field (i.e. topic) with a delimiter before and after each value like:
(1)(2)(3)(14)(15)(255)(283)
If you did something like this for the topic field, the query is simple ...
select * from table where topic like "%(13)%";

How to check data for duplication in database?

I have 3 fields of topic name in 3 different languages. When entering new topic I'm using JavaScript to show on the page if that topic exists already in the database by using select and like clause statement. The problem is it finds any duplication if the topic name entered exactly the same, but not if only some words are the same.
How do I check for duplication on topic that checks the words for similarity and not the whole topic?
SQL Server provides Full Text Search feature. You may try for this instead of your like clause.
ATaylor pretty much spells it out for you. To exclude the specific common keywords, I would recommend using an "EXCEPT SELECT WHERE LIKE" statement at the end of your statement.
SELECT DISTINCT(field) AS var1, COUNT(field) AS var2 FROM table GROUP BY field HAVING var2 > 1;
This would show up any field that exist twice or more in your current database
Full Text Search, you can have choice on you relevancy score. write a query to get the most relevant results.
SELECT MATCH('content') AGAINST ('keyword1
keyword2') as relevancy_score FROM topic WHERE MATCH
('Content') AGAINST('+keyword1 +keyword2' IN
BOOLEAN MODE) HAVING relevancy_score > 0.2 ORDER
BY relevancy_score DESC.
you may refer to this link.
http://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html

mysql search database against a query

I'm looking for a way to compare database values against a given query using MySql
(in oppose of searching the query in the db)
I will elaborate:
I have a table that holds comma separated keywords and a result for each block of keywords
for example :
col 1 col 2
Mercedes,BMW,Subaru car
Marlboro,Winston cigarette
today im taking the user query (for example - Marlboroligt)
as you can see if i will search for the value 'Marlboroligt' in the db i will get no results.
for that matter i want to search 'Marlboro' from the db inside the query and return 'cigarette'
Hope my explanation is sufficient and that this is actually possible :-).
Normalization will help you the most. Otherwise, search in comma delimited fields is discussed here:
Select all where field contains string separated by comma
...and to search 'Marlboroligt' instead of 'Marlboro Light', you can try looking into the LEVENSHTEIN function, or maybe the Soundex encoding (which looks like too little bang for too large a buck, but then again, maybe...).
I see the following possible solutions:
setup a keyword-search engine like Sphinx and use it to search keywords in your db
normalize your db - col1 must contain the only keyword
use like patterns
select col2 from mytable where col1 like "%Marlboro%"
like slows down your application and can have substring-related issues.

MySQL - Filter result

I want to filter result of my SQL query. I want to select everything that has some specific text in some column.
Example:
SELECT * FROM categories WHERE (name
has 'abc' values in it's value ex.
MyabcCategory)
Also maybe it is not very good idea to do that in query, maybe it is better to get all and then filter array instead? But I don't know how to do that aether.
Use LIKE with % wildcard:
SELECT * FROM categories WHERE name LIKE '%abc%'
This will give you all the records that have abc somewhere in them.
You can learn more about it here :)
You want to use the LIKE operator, with % to match any character before and after your specific word :
select *
from categories
where name like '%abc%';
But note that doing so, MySQL will scan each line of the table, every time the query is executed... which might not be great if you have a lot of data.
If you're searching for some kind of text, you might either want to :
Use a FULLTEXT index, if you're working with MyISAM tables.
Or, use a solution that's separated from MySQL, with a specific indexing/search engine, such as Solr.
SELECT * FROM categories WHERE name LIKE '%abc%'

Categories