what is the difference between LIKE and REGEXP? - mysql

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

Related

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

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

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 '!'

MySQL query: search multiple strings in a single field, from CSV in other field/table?

I have a table that contains two TEXT fields with textual content, and in a separate table, a field that contains comma separate values of keywords that can be more than one word. The following query works in my WAMP using Appserv, does not works in our Hostgator LAMP...why??
SELECT
t.content_me, t.content_visitor, t.Id, exp.owner_user_id, exp.name
FROM (SELECT t.content_me, t.content_visitor
FROM `texts` AS t
WHERE t.owner_user_id=1 *<== obviously this changes...*
ORDER BY t.Id DESC) AS t
INNER JOIN exp ON t.Id = exp.owner_user_id AND
t.content_me REGEXP (REPLACE(exp.keywords,',','|'))
WHERE t.owner_user_id=e.owner_user_id=6
ORDER BY t.Id DESC
Furthermore, if I literally put a value in this part:
t.content_me REGEXP (REPLACE(exp.keywords,',','|'))
as lets say:
t.content_me REGEXP ('yeah|ok')
It works in Hostgator. So I guess the problem is that REGEXP (REPLACE(exp.keywords,',','|')) thingy...right?
EDIT
Ok, I simplified the query just for the fun of it :)
SELECT
t.Id, t.text, t.owner_user_id FROM `t`
LEFT JOIN e ON e.owner_user_id = t.owner_user_id
WHERE
t.text REGEXP REPLACE(e.keywords,',','|') AND t.owner_user_id=1
Same results: works in WAMP, doesn't in LAMP. Also if I do literal REGEXP like
REGEXP REPLACE('yeah,can',',','|')
Works in both servers. My guess is that something is happening with
REGEXP REPLACE(e.keywords,',','|')
i.e, having the REGEXP use field content and not literal sting.
EDIT:
Well...now I see the LAMP MySQL throws an error (not happening in WAMP):
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation 'regexp'
So....
REGEXP REPLACE(e.keywords,',','|') COLLATE utf8_unicode_ci
Fixed it

MySQL select statement remove whitespace in WHERE clause

I'm trying to match phone numbers based on the last 6 digits. The problem is the numbers in the database are in various formats, some have whitespace within the number.
SELECT * FROM users WHERE trim(phone) LIKE '%123456'
Trim only removes the leading and trailing whitespace and doesn't find entries where clients have entered their numbers with whitespace between the numbers:
123 456, 12 34 56, etc.
So how to remove the whitespace within the search? Having the result without whitespace is not enough. Updating the database with replace is not an option either.
Use replace() to substitute all occurrences of ' ' to '' within a string.
mySQL documentation
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.
SELECT * FROM users WHERE Replace(coalesce(Phone,''), ' ','') LIKE '%123456'
If you can't do it in the where clause, which seems odd to me; just nest it in a select.
Select sub.*
from (Select u.*, Replace(coalesce(Phone,''), ' ','') as ph
from users u) sub
where sub.ph LIKE '%123456'

My sql:how to use Wildcard in Subquery?

example:
SELECT country
FROM data
WHERE city LIKE
(SELECT LEFT ('jakartada',7));
it's working nicely but if i give delimiter with value:4 ,i must give wildcard
like this ---> "%string%" ,where i can give wildcard in the query?
With LIKE you can use the following two wildcard characters in the pattern.
Character Description
% Matches any number of characters, even zero characters
_ Matches exactly one character
\% Matches one “%” character
\_ Matches one “_” character
does this query help you?
SELECT country
FROM data
WHERE city LIKE CONCAT ('%', (SELECT LEFT ('jakartada',7)), '%');
why you using sub query?
SELECT * FROM `data` WHERE city LIKE 'jakar%'