What should i need pay attention to in mysql query, such as %? - mysql

For example, Search is a commonly used functions.
I will generate a query string like "SELECT * FROM table WHERE col LIKE '%{keyword}%'"
but the keyword's value is '%', it does not work, how to fix it.
In addition, what other characters need to be aware of?

Just use a backslash to escape the percent. For example, if your keyword were 5% discount, then you would use:
SELECT * FROM table WHERE col LIKE '%5\% discount%'
The documentation lists only two characters which have a special meaning in LIKE:
% - matches any number of characters
_ - matches any single character
If you want to use either of these characters literally in a LIKE expression, then you will need to escape them.

Related

Why isn't MySQL REGEXP filtering out these values?

So I'm trying to find what "special characters" have been used in my customer names. I'm going through updating this query to find them all one-by-one, but it's still showing all customers with a - despite me trying to exlude that in the query.
Here's the query I'm using:
SELECT * FROM customer WHERE name REGEXP "[^\da-zA-Z\ \.\&\-\(\)\,]+";
This customer (and many others with a dash) are still showing in the query results:
Test-able Software Ltd
What am I missing? Based on that regexp, shouldn't that one be excluded from the query results?
Testing it on https://regex101.com/r/AMOwaj/1 shows there is no match.
Edit - So I want to FIND any which have characters other than the ones in the regex character set. Not exclude any which do have these characters.
Your code checks if the string contains any character that does not belong to the character class, while you want to ensure that none does belong to it.
You can use ^ and $ to check the while string at once:
SELECT * FROM customer WHERE name REGEXP '^[^\da-zA-Z .&\-(),]+$';
This would probably be simpler expressed with NOT, and without negating the character class:
SELECT * FROM customer WHERE name NOT REGEXP '[\da-zA-Z .&\-(),]';
Note that you don't need to escape all the characters within the character class, except probably for -.
Use [0-9] or [[:digit:]] to match digits irrespective of MySQL version.
Use the hyphen where it can't make part of a range construction.
Fix the expression as
SELECT * FROM customer WHERE name REGEXP "[^0-9a-zA-Z .&(),-]+";
If the entire text should match this pattern, enclose with ^ / $:
SELECT * FROM customer WHERE name REGEXP "^[^0-9a-zA-Z .&(),-]+$";
- implies a range except if it is first. (Well, after the "not" (^).)
So use
"[^-0-9a-zA-Z .&(),]"
I removed the + at the end because you don't really care how many; this way it will stop after finding one.

How to use like condition in query to fetch strings that contains % symbol

I have 3 strings entries in my table sample and column test
1.abc
2.abc%d
3.abc%E
Now I want to write a query to fetch all the records in column test that contains abc% using like condition. The output should be abc%d and abc%E.
Have you used the "_" underscore character in SQL query?
This may help to resolve your issue.
select * from myTableName where details like 'abc%_%'
or
select * from myTableName where details like 'abc%_'
or
select * from myTableName where details LIKE '%abc\%%' ESCAPE '\'
or
select * from myTableName where details LIKE 'abc\%%' ESCAPE '\'
All the above queries will solve your issue, use the appropriate query based on your application need and requirement.
Reference: Use Underscore character in wild card charecter of Like query gives me all table result
As stated in the documentation, you must escape instances of wildcard characters if you do not want them to behave as such.
To test for literal instances of a wildcard character, precede it by
the escape character. If you do not specify the ESCAPE character, \ is
assumed.
\% matches one % character.
\_ matches one _ character.

matching escape charactres using like operator in mysql

I want to match the string having escape characters with particular column in a table.
SELECT * FROM table WHERE col LIKE 'MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH';
Though there is matching data in table, query result is empty set. But the same query works fine in oracle. What is the issue with mysql?
You miss %:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH%';
But it should work without escaping:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\MESSVEGEN 1\STOCKH%';
Fiddle http://sqlfiddle.com/#!9/a7ba59/2
EDIT:
SELECT * FROM t WHERE n LIKE '%MESSRESTAURANGER AB\\\\\\\\MESSVEGEN 1\\\\\\\\STOCKH%'
Because MySQL uses C escape syntax in strings (for example, “\n” to
represent a newline character), you must double any “\” that you use
in LIKE strings. For example, to search for “\n”, specify it as “\n”.
To search for “\”, specify it as \\\\; this is because the
backslashes are stripped once by the parser and again when the pattern
match is made, leaving a single backslash to be matched against.
Fiddle http://sqlfiddle.com/#!9/ac46b/9

MySQL \ regexp \ Search only the records which contains specific characters

I would like to find all records from 'mytable' which in the field 'name' used only characters listed below:
Ø-*³`!/-;$€"“- „”\ø,Ø:’.#*+_/? !œ³¥Φ?+#=–()<>ąĄćĆęĘłŁńŃóÓśŚż ŻźŹàáâåéÉêéíıñçãėÊèÈçßœŒæğîïİşúūýōòÒô
regular letters from a to z (and A to Z)
number 0,1,3,4,5,6,7,8,9
spaces and some 'tab' signs
This query does not work:
SELECT name
FROM mytable
WHERE name not regexp '[^a-zA-Z0-9Ø-*³`!/-;$€"“- „”\ø,Ø:’.#*+_/? !œ³¥Φ?+#=–()<>ąĄćĆęĘłŁńŃóÓśŚż ŻźŹàáâåéÉêéíıñçãėÊèÈçßœŒæğîïİşúūýōòÒô]'
I know that this solution is far from good :) but I've tried different methods - this one returns the result closest to the required. Can you please give me some hint?
You can take advantage of character classes.
For example, instead of [ąĄóÓōòÒô...] use [[=A-Za-z=]].
This will match any letters from a through z igoring case and ignoring if the letter has accent.
Check the documentation for additional characters classes that will match your missing characters.

SQL wildcard Oddities, need stricter results

SELECT *
FROM list
WHERE list_item LIKE "%_1375_%"
returns results it should not. For example a result with 13753 in the list_item instead of 1375, even though the 3 comes before the underscore.
_13753_ and _1375_ are written above and not suppose to be italic.
Is there anyway to fix that?
From MySQL reference manual here
SQL pattern matching enables you to use “_” to match any single
character and “%” to match an arbitrary number of characters
(including zero characters).
So, you'll have to escape them to match a literal underscore character. Like so:
SELECT * FROM list
WHERE list_item LIKE "%\_1375\_%"
Underscore means any character. Are you looking for values with an underscore? In that case you need to escape it
SELECT *
FROM list
WHERE list_item LIKE "%\_1375\_%"
Try this one
LIKE '%[_]1375[_]%'