Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql wildcards % vs %%

What is the difference in '%' and '%%', when used in mysql where clause with 'LIKE' ?

select * from `wp_users` u where u.user_nicename like "%lastuser%"

VS

select * from `wp_users` u where u.user_nicename like "%%lastuser%%"
like image 588
Riz Avatar asked Dec 23 '11 13:12

Riz


People also ask

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What's the difference between _ and in SQL wildcard?

What's the difference between _ and in SQL wildcard? % (Percentage) – Using this wildcard operator, we can match from 0 upto many characters in a string or column's value. _ (Underscore) – Using this wildcard operator, we can only match one character in a string or column's value.

Is * a wildcard in SQL?

To broaden the selections of a structured query language (SQL-SELECT) statement, two wildcard characters, the percent sign (%) and the underscore (_), can be used. The percent sign is analogous to the asterisk (*) wildcard character used with MS-DOS.

What are the two types of wildcards?

The most common wildcards are the asterisk (*), which represents one or more characters, and question mark (?), which represents a single character.


1 Answers

There is no difference between %% and % when it comes to pattern matching in mysql.

I've seen developers get confused over this when they try to match a literal % and therefor write %%. This is most often because of the fact that format-strings often use a double % to indicate that you'd like it to be treated as an exact literal.


MySQL documentation of LIKE

  • MySQL 5.0 Reference Manual :: 11.5.1 String Comparison Functions :: LIKE

What's the origin of the string, and where is it going?

If the string is passed to a function such as sprintf the format-string rule I mentioned earlier is present, though there is no confusion in that case.

The developer want it to be a single % in the string passed to mysql, and therefor wrote %%.

$query = sprintf (
  "SELECT ... FROM ... WHERE id <> %d AND data LIKE '%%hello world%%'",
  50
);

// $query => "SELECT ... FROM ... WHERE id <> 50 AND data LIKE '%hello world%'";

A few sample SELECTs using the LIKE operator

mysql> SELECT 'abc' LIKE 'ab%';
+------------------+
| 'abc' LIKE 'ab%' |
+------------------+
|                1 |
+------------------+
1 row in set (0.01 sec)

mysql> SELECT 'abc' LIKE 'ab%%';
+-------------------+
| 'abc' LIKE 'ab%%' |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

mysql> SELECT 'abc' LIKE 'ab\%';
+-------------------+
| 'abc' LIKE 'ab\%' |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)

mysql> SELECT 'ab%' LIKE 'ab\%';
+-------------------+
| 'ab%' LIKE 'ab\%' |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)
like image 130
Filip Roséen - refp Avatar answered Nov 11 '22 15:11

Filip Roséen - refp