MySQL search without any special characters - php

I am wondering if it's possible to search through a MySQL field where the field may have something like this:
test - hello
but you have in a string from a user
test: hello
Obviously it's easy on PHP-side to strip the string of any special characters like that, but is it possible to search through MySQL rows and ignore special characters?

Another unique solution is to put wildcards in between each word. As long as the search phrase does not have special characters in it, the correct results will be returned while ignoring any special characters in the results.
For example...
SELECT *
FROM myTable
WHERE somefield LIKE '%test%hello%'

It could be possible if you find and replace all such special character and spaces from user input also column,
i.e.
select * from tablename where replace(replace(columnname,' ',''),':',''),'-','')=replace(replace([USER INPUT],' ',''),':',''),'-','');

You can sort of "ignore" special characters, whitespace, etc. by using the SOUNDEX() function:
mysql> select soundex('test - hello'), soundex('test: hello');
+-------------------------+------------------------+
| soundex('test - hello') | soundex('test: hello') |
+-------------------------+------------------------+
| T234 | T234 |
+-------------------------+------------------------+
So you can search your data like this:
SELECT ...
FROM MyTable
WHERE SOUNDEX(somefield) = SOUNDEX('test: hello');
This won't be indexable at all, so it'll be forced to do a table-scan. If you use MySQL 5.7, you could add a virtual column for the soundex expression, and index that virtual column. That would help performance a lot.

Related

How to implement Full Text search in InnoDB?

I have a query,
e.g.
name column have "Rodrigue Dattatray Desilva".
I want to write a query in such a way that,
If I search for 'gtl' and match anywhere in string it should show the result.
I know in PHP I can apply the patch like '%g%t%l%'.
But I want to know MySql way.
Note: I can search for anything, I am just giving above an example.
EDIT:
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Rodrigue Dattatray Desilva");
select * from Test where title like '%g%t%l%';
Consider the above case. Where "gtl" is string I am trying to search in the title but search string can be anything.
gtl is string where it exists in the current title but not in sequence.
The easy answer is that you need an extra wildcard:
select * from Test where title like '%g%t%l%';
The query you posted does not have a wild card after the 'l', so would only match if the phrase ended with 'l'.
The more complicated answer is that you can also use regular expressions, which give you more power over the search.
The even more complicated answer is that performance of these string matching queries tends to be poor - the wild cards mean that indexes are usually ineffective. If you have a large number of rows in your table, full-text searching is much faster.
You can do the same in Mysql too.
You can use the keyword like in MySql.
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character

Mysql Match against query with special charters search

I have following match against query which searches records from database table based on search phrase.
SELECT * FROM My_Table WHERE MATCH (catchall) AGAINST ('"horse"' IN BOOLEAN MODE)
This query works properly. When search phrase contains special characters like '(' etc It just skips such special characters.
If i search for "(horse)" it gives me same result as it gives for "horse".
SELECT * FROM My_Table WHERE MATCH (catchall) AGAINST ('"(horse)"' IN BOOLEAN MODE)
Does it mean match against query doesn't work with special characters or am i missing something. Please suggest. Thanks.
I tried by removing IN BOOLEAN MODE from the query but it didn't work.
from the documentation:
Parentheses group words into subexpressions. Parenthesized groups can be nested.
if you want to treat prenthes as "word chars", there are two possibilitys:
If you want to change the set of characters that are considered word
characters, you can do so in two ways. Suppose that you want to treat
the hyphen character ('-') as a word character. Use either of these
methods:
Modify the MySQL source: In myisam/ftdefs.h, see the true_word_char()
and misc_word_char() macros. Add '-' to one of those macros and
recompile MySQL.
Modify a character set file: This requires no recompilation. The
true_word_char() macro uses a “character type” table to distinguish
letters and numbers from other characters. . You can edit the
contents in one of the character set XML files to specify
that '-' is a “letter.” Then use the given character set for your
FULLTEXT indexes.
After making the modification, you must rebuild the indexes for each
table that contains any FULLTEXT indexes.
a third way would be to not use MATCH ... AGAINST at all and use LIKE instead - but this might get complicated (if you want to use the other operators of ful-text-searches such as +/-) and slow down your query.

mysql query that looks for a value in a string

I am wondering if i can put something in my query which searches for a word within a string. For example, if i have a field named user_purchased, i want to query it and pull only records where the word 'dog' exists within the string.
I know i can do this using php like this
(!stristr($row['user_purchased'], 'dog'))
BUT, i'm wondering if i can do this in the query itself instead and that this will perhaps speed up performance.
Thanks in advance for help on this
You can use LIKE:
WHERE your_col LIKE '%dog%'
If you want better performance you could look at a full text search. This uses a FULLTEXT index to quickly find the matching rows.
In MySql, you can use INSTR(str,substr)
Quote from the MySql manual at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
INSTR(str,substr)
Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.
mysql> SELECT INSTR('foobarbar', 'bar');
-> 4
mysql> SELECT INSTR('xbar', 'foobar');
-> 0
This function is multi-byte safe, and is case sensitive only if at least one argument is a binary string.
SELECT * FROM table WHERE user_purchased LIKE '%dog%';

PHP search function to look in pgsql table

I want to make a search function in my website. I want to search for a string in all fields of my table (about 13 columns). If one row contains a field that matches the string (like operator) I want it to be added to result.
Example
|field 1 | field 2 | field 3|
some string test
test some string
one simple string
Now basically if I search for the string "test" I want to have the first two rows.
Is there a wildcard option for WHERE that I could do something :
SELECT * from my.table WHERE * like '%string%';
There is no such syntax in PostgreSQL (or any other DBMS).
As Spudley pointed out using a query like like '%string%' will be quite slow.
If this is something that is needed very often you should definitely look into PostgreSQL's full text search capabilities.

Storing regular expressions in MySQL database table and maching against them

I have a very interesting task, which I don't know how to implement.
I need to store many regular expressions in a database table, and need to be able to find which of them matches the given string.
For example:
id | regexp
---|-------------
1 | ^hello world$
2 | ^I have [0-9] flowers&
3 | ^some other regexp$
4 | ^and another (one|regexp)$
And I need to find which of those expressions matches string "I have 5 flowers". Of course I can SELECT * FROM table and loop through an expressions matching them one by one in PHP, but this would be horrible for server to handle.
Can I somehow index this table or use a special SQL query to handle this task?
I'll appreciate any answer. Thank you.
select * from table where $your_string RLIKE regexp
mysql regular expressions
SELECT * FROM table WHERE 'some stuff' REGEXP `regexp`;
Unfortunately there is no way to use indexes with queries that use regexps.

Categories