MySQL: What does means "escape '!'" on query - mysql

Editing someone else's code found this query:
SELECT c.name AS category_name,
p.id,
p.name,
p.description,
p.price,
p.category_id,
p.created
FROM products p
LEFT JOIN categories c
ON p.category_id = c.id
WHERE p.name LIKE '%keyword%' escape '!'
OR p.description LIKE '%keyword%' escape '!'
ORDER BY p.name ASC
LIMIT 0, 6
I understand everything but the escape '!' on lines 11 and 12. I guess is something related to 'escaping' and, in case of, don't know if is better implementing it before the query (code soup is PHP) or let the job to the DB engine (And what means the '!' symbol?).
Thanks in advance.

The ESCAPE keyword is used to escape pattern matching characters such as the (%) percentage and underscore (_) if they form part of the data.
Let's suppose that we want to check for the string "67%" we can use;
LIKE '67#%%' ESCAPE '#';
If we want to search for the movie "67% Guilty", we can use the script shown below to do that.
SELECT * FROM movies WHERE title LIKE '67#%%' ESCAPE '#';
Note the double "%%" in the LIKE clause, the first one in red "%" is treated as part of the string to be searched for. The other one is used to match any number of characters that follow.
The same query will also work if we use something like
SELECT * FROM movies WHERE title LIKE '67=%%' ESCAPE '=';

You don't need escape in this particular query but if you ever do (i.e. if you have the % character in your search term) you will need a escape character to differentiate between the % which is part of your search term and other % characters that serve as placeholder in the like part of the query.
In that case, you escape the % in your search term with the character you defined using the escape keyword.
For instance, say you want to look for the string 25% in the name field. You'll go like this:
WHERE p.name LIKE '%25!%%' escape '!'

Related

how to use keywords to search column in sql

I need to find all the songs which has 'ing' in song_title.
So, I write following code:
select *
from song
where song_title = 'ing'
But, that shows all the song_title which name is 'ing'.
How to get all that contain's 'ing' in song_title?
Use LIKE operators for pattern matching as follows:
SELECT* FROM song WHERE song_title LIKE '%ing%'
If you want to select records that start with a certain string then you can use:
SELECT* FROM song WHERE song_title LIKE 'ing%'
If you want to select records that end with a certain string then you can use:
SELECT* FROM song WHERE song_title LIKE '%ing'
As an alternative to a LIKE comparison, MySQL also has a REGEXP comparison operator. To return only rows where song_title contains the string 'ing'
WHERE song_title REGEXP 'ing'
With a LIKE comparison, the wild card characters are '%' to match any number (zero, one or more) of any character, and the underscore '_' to match one character. To search for literal '%' or '_' character, those characters need to be escaped.
With the REGEXP, there's a whole boatload of characters that have special meaning. e.g. '^' matches the beginning of the string, '$' matches the end of the string, and the square brackets, and all those other characters frequently used in regular expressions.
select * from song where song_title like '%ing%';
Use the like operator with substitution strings.

MySQL - need to find records without a period in them

I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';

mysql statement not to include an underscore in results

I have a mysql statement like this
$sql_insertwotd = "SELECT * FROM table WHERE word != '' AND word NOT LIKE '%\_%' ORDER BY RAND() LIMIT 1";
for some reason it it will still pull up a word with an underscore. I read this is the proper format to clarify an underscore with a .
I dont want it to select a word that contains an underscore
You have to use TWO BACKSLASHES (\\) and it should work
SELECT * FROM table WHERE word != '' AND word NOT LIKE '%\\_%'
ORDER BY RAND() LIMIT 1
You may need an explicit escape:
word NOT LIKE '%/_%' ESCAPE '/'
https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
By default \ is the escape character and your query should work as is.

what is the difference between LIKE and REGEXP?

I have a mySQL wildcard query that isn't working probably because I use InnoDB instead of MyISAM.
SELECT a.product_id, a.category_id FROM products a
LEFT JOIN users u ON u.userid=a.ownerid
WHERE a.active=1 AND a.approved=1
AND a.deleted=0 AND a.name LIKE '%*my*%'
AND a.name LIKE '%*donuts*%'
AND (a.name REGEXP '( )*(*my*)*( )*(*donuts*)( )*')
It works fine whenever a word is used instead of a wildcard, that's not the problem.
I'm just wondering, is the part of
(a.name REGEXP '( )*(*my*)*( )*(*donuts*)( )*')
really needed after already doing a
LIKE '%*my*%' AND a.name LIKE '%*donuts*%'
What is the difference?
What is the difference?
The regular expression
(a.name REGEXP '( )*(*my*)*( )*(*donuts*)( )*')
This produces an error, because a quantifier (e.g. an unescaped * character) is not valid at the start of a group (i.e. immediately after an unescaped ( character). Ignoring the two times where that occurs yields:
(a.name REGEXP '( )*(my*)*( )*(donuts*)( )*')
This matches any a.name with:
zero or more spaces, followed by
zero or more occurrences of:
the letter m, followed by
zero or more letters y
followed by
zero or more spaces, followed by
the character sequence donut, followed by
zero or more letters s, followed by
zero or more spaces
The simple pattern
a.name LIKE '%*my*%' AND a.name LIKE '%*donuts*%'
This matches any a.name with:
any sequence of characters, followed by
the character sequence *my*, followed by
any sequence of characters
and with:
any sequence of characters, followed by
the character sequence *donuts*, followed by
any sequence of characters

SQL Query where a field value does not contain empty spaces

I am currently using the follow query:
SELECT *
FROM `wp_usermeta`
WHERE meta_key='avatar'
AND meta_key NOT LIKE '% '
ORDER BY RAND()
LIMIT 4
In that way, I want to try to get only field values, where no empty spaces re in the file name. Where is the error in my query? It still selects filenames with empty spaces in the filename.
Try
NOT LIKE '% %'
Your current wildcard match only catches trailing spaces.
Also, you're using meta_key twice. Should the column used in your LIKE clause be meta_value (or whatever it is in Wordpress).
This question is probably worth reading if you're concerned about performance - Which is faster — INSTR or LIKE?