Mysql select query can't able to search symbolic string - mysql

For example if I want to search below.
> lav'ro (I can't search it by "lav'r" because of ' in the string)
> am[op]st (I can't search it by "am[o" because of [ in the string)
My question is how can I use select query so I will help me to bring output?
Below are my SELECT query.
SELECT * FROM TableName
WHERE (Col1 LIKE '%$value%' OR Col2 LIKE '%$value%')
ORDER BY
CASE WHEN (Col1 LIKE '$value' AND Col2 LIKE '$value') THEN 1
WHEN Col1 LIKE '$value' THEN 2
WHEN Col2 LIKE '$value' THEN 3
WHEN Col1 LIKE '$value%' THEN 4
WHEN Col2 LIKE '$value%' THEN 5
WHEN Col1 LIKE '%$value' THEN 6
WHEN Col2 LIKE '%$value' THEN 7
WHEN Col1 LIKE '%$value%' THEN 8
WHEN Col2 LIKE '%$value%' THEN 9
ELSE 10
END asc,
length(Col1 or Col2) limit 15
Here "$value" dynamic word for search.
It's working well In normal search, problem with symbolic search.

You have to include \ before the single quote to make it work
SELECT * FROM `table` WHERE `column` LIKE 'lav\'ro'
For the 2nd issue try % to append before or after or both
SELECT * FROM `table` WHERE `column` LIKE '%am[o%'

You don't have to use like. You can just use instr() or =:
where instr(col1, ?) > 0
Use like and regular expressions when you need them for their pattern matching capabilities.
And -- for the record -- there is no problem searching for single quotes in a string. The only problem is expressing the string. To do this, double up on the single quote:
where col1 like concat(?, '%')
works when you pass a parameter with a single quote. This also works:
where col1 like 'lav''r%'

Related

mysql how to compare strings and return rows between two strings

How can I simply filter a table row by alphabets for example showing only rows with strings starting with e,f and g and exclude other rows.
Something like
return (rows > "d") andalso (rows < "h")
in vb.net
you can use like if you want regular expression. for example, this will give you all rows starting with a:
select * from myTable where myColumn like 'a%';
EDIT:
I see what you mean now, mysql supports > and < for strings as well, so if you do:
where col >= 'e' and col < 'h'
you will essentially get everything starting by e,f,g
You can combine multiple LIKE operator saying
select * from table1
where mycol like 'e%'
or mycol like 'f%'
or mycol like 'g%';
(OR) you can use LEFT() string function saying
select * from table1
where left(mycol,1) in ('e','f','g');

sql query using LIKE or MATCH

I have a table of 13 columns , out of those i have 5 columns that contains let say string data or VARCHAR data.
Now i have a string let say "abc".
I want to write a sql query to get all the rows that have this "abc" string in those 5 columns. "abc" can be the data of the column of part of the data of the column.
I used LIKE function
SELECT * FROM table WHERE col1 OR col2 OR col3 OR col4 OR col5 LIKE '%abc%';
but it didnt worked n got back all the rows.
I dont know how use MATCH function either, I m nt good in sql. so can anyone help.
You can specify condition multiple times:
SELECT *
FROM table
WHERE col1 LIKE '%abc%'
OR col2 LIKE '%abc%'
OR col3 LIKE '%abc%'
OR col4 LIKE '%abc%'
OR col5 LIKE '%abc%';
This will be really slow because you have multiple OR and non-SARGable condition.
Alternatively:
SELECT *
FROM table
WHERE CONCAT_WS('^', col1,col2,col3,col4,col5) LIKE '%abc%';
Using MATCH (preferred solution that utilizes full-text index):
SELECT *
FROM table
WHERE MATCH(col1, col2,col3,col4, col5) AGAINST ('abc');
SqlFiddleDemo
Keep in mind that to use MATCH you need to create index first:
ALTER TABLE tab ADD FULLTEXT ft_index_name (col1,col2,col3,col4,col5);

MySQL regular expression to return all records where at least one word matches between 2 columns

My database has 2 columns that contain text. When it has say 3 records as follows:
rec# col1 col2
1 my name is fred is
2 john mike
3 today not sat not it sat
I would appreciate help constructing a regular expression that will return record numbers:
1 -> because "is" matches
3 -> because "'not" and "sat" match (i.e. at least one match exists)
I think you can do this as:
select t.*
from table t
where col1 rlike replace(col2, ' ', '|');
This turns col2 into a regular expression. So, note that this answer will be very sensitive to the contents of col2. If it contains regular expression special characters, then this probably will not work.
What do you mean by "matches"?
This SELECT will find the rows where col1 contains at least one of the words is or not or sat:
SELECT rec_num
FROM tbl
WHERE col1 REGEXP '[[:<:]](is|not|sat)[[:>:]]';
This says that at least one of those words exists in both col1 and col2:
SELECT rec_num
FROM tbl
WHERE col1 REGEXP '[[:<:]](is|not|sat)[[:>:]]'
AND col2 REGEXP '[[:<:]](is|not|sat)[[:>:]]';
Change AND to OR to ask if one of the words exists in either (or both) column.
If you need the same word (is/not/sat) to match both col1 and col2, that is more complex:
SELECT rec_num
FROM tbl
WHERE ( col1 REGEXP '[[:<:]]is[[:>:]]'
AND col2 REGEXP '[[:<:]]is[[:>:]]' )
OR ( col1 REGEXP '[[:<:]]not[[:>:]]'
AND col2 REGEXP '[[:<:]]not[[:>:]]' )
OR ( col1 REGEXP '[[:<:]]sat[[:>:]]'
AND col2 REGEXP '[[:<:]]sat[[:>:]]' );
If you mean something else, practice asking precise questions.
Addenda
It is not practical in SQL to discover which words (if any) are in common between two text fields. Such a task would be better done in an application programming language (PHP, Java, Perl, ...).

Multiple Column REGEX search in MySQL

I am trying to search multiple columns in my Db using a regex. It works but using many and/or statments. I was wondering if it was possible to use something like this;
SELECT * FROM table REGEXP 'regex' IN (col1, col2, col3,.....)
This doesn't work, it was a guess at the syntax because I can't find anything similar by searching online. Is this a stupid idea or am I missing something very simple?
If you want to regexp search a value in multiple columns then you can do:
SELECT * FROM table where CONCAT(col1, col2, col3) REGEXP 'search-pattern';
The syntax for MySQL REGEX comparison is
expr REGEXP pattern_string
You cannot use it with IN. You would have to do:
SELECT * FROM `table` WHERE
col1 REGEXP 'regex'
OR col2 REGEXP 'regex'
OR col3 REGEXP 'regex'
You could also use RLIKE -- they are synonyms.

CASE zip LIKE MySQL

I am creating a "simple" search query. I'd like to select a column that should have the value 1 if a specified column LIKE('test'), it's kind of hard to explain.
What i want to do is like this
SELECT *,(CASE mycol LIKE('%test%') as match THEN 1 END) FROM mytable
So if mycol matches the condition, then match=1, if else 0. How would i do that? Sorry if hard to understand.
You are nearly there, but you have made four errors and used one bad practice:
Add ELSE 0 in your CASE expression.
CASE expressions need one or more WHEN expressions.
Put the alias in the correct place - after the end of the expression.
Don't use the reserved word match as an alias.
Also, don't use SELECT *.
Try this:
SELECT
col1,
col2,
...,
coln,
CASE WHEN mycol LIKE '%test%' THEN 1 ELSE 0 END AS zipmatches
FROM mytable
However there's no need for the CASE expression in MySQL because TRUE and 1 are equivalent:
SELECT
col1,
col2,
...,
coln,
mycol LIKE '%test%' AS zipmatches
FROM mytable
Here is another expression
SELECT *, IF(mycol LIKE('%test%'),1,0) is_a_match FROM mytable;