SQL wildcard
Only WHERE data lookup function, it functions a bit too simple, then we introduced a wildcard. Matching a wildcard operator is LIKE function.
Wildcard search for a long time, so generally try not to search for other alternatives equivalent to a wildcard, even to use the word, but also should try to write in a statement after the search, rather than the beginning.
% Wildcard
% Represents any characters appear any number (0, 1 times, n being times). For example, the following expression is to search for the beginning of the line behind the jet with any character
SELECT * FROM table WHERE colname LIKE ‘jet%’;
The following indicates the row containing jet at an arbitrary position of the search
SELECT * FROM table WHERE colname LIKE ‘%jet%’;
You can also search a b-line to the beginning of the end,
SELECT * FROM table WHERE colname LIKE ‘a%b’;
Note: Wildcards% does not match NULL value.
And case sensitive wildcard
- Wildcard
And different% - matching a single character
The SELECT * the FROM Table the WHERE colname The the LIKE 'Jet-'; match is Jeta, jetb, jetc
the SELECT * the FROM Table the WHERE colname The the LIKE 'Jet -'; match is jet a, jet b, jet c
Regular Expressions
Regular expression applied to more complex search scene. Regular expressions to match operator REGEXP , regular expression case insensitive and, if need may REGEXP After BINARY
The basic character matches
We start with a search of the beginning of jet
SELECT * FROM table WHERE colname REGEXP ‘jet’;
And if you want to match jeta jetb, we must use to, it matches any one character, and wildcards - the same role
SELECT * FROM table WHERE colname REGEXP ‘jet.’;
Conduct or match
Hereinafter expressed on a column containing jeta, jetb line by searching | expressed or
SELECT * FROM table WHERE colname REGEXP ‘jeta|jetb’;
SELECT * FROM table WHERE colname REGEXP ‘jeta|jetb|jetc’;
Matches any single character
Use [] to match any single character
The SELECT * the FROM Table the WHERE colname The REGEXP, 'Jeta | jetb | jetc';
equivalent to
the SELECT * the FROM Table the WHERE colname The REGEXP, 'Jet [ABC]';
[ABC] is [a | b | c] shorthand
the SELECT * the FROM Table the WHERE colname The REGEXP, 'Jet [^ ABC]';
any number of matches except jeta jetb jetc is expressed.
Matches any number within the range
[0123456789] can be simplified to [0-9]
Matching special characters
The use of separate words used to represent any single character, but if we want to search included. The statement, to use \. To represent lookup.
SELECT * FROM table WHERE colname REGEXP ‘\.’;
We call it an escape for all symbols have a special role in SQL regularization of expression.
Metacharacters | Explanation |
---|---|
\f | Feed |
\n | Wrap |
\r | Enter |
\t | tabulation |
\ v | Vertical tab |
Matching character class (class)
class | Explanation |
---|---|
[: Scooping] | alpha(upper or lower) and number[0-9] |
[:alpha:] | As the name suggests all uppercase and lowercase letters |
[:blank:] | Spaces and tabs |
[:digit:] | Any number [0-9] |
[:print:] | Any printable character |
[:graph:] | In addition to any space printable characters |
[:lower:] | Lowercase letters [az] |
[:upper:] | Uppercase letters [AZ] |
[:space:] | Matching metacharacters |
Examples of the plurality of matching
Previously mentioned are any number appears to match a single location, and we often need to find more complex cases, this time we duplicate meta characters.
Metacharacters | Explanation |
---|---|
* | Zero or more matches |
+ | One or more matching |
? | 0 or a matching |
{n} | n matches |
{n,} | Less than n-matching |
{n,m} | Number of matching range (m <255) |
The SELECT * the FROM Table the WHERE colname The REGEXP, '\ ([0-9] Sticks \?)';
Expression search 1 stick, 2stick or 1 sticks, 2sticks.
? Followed represents the stick or stick search Sticks
the SELECT * the FROM Table the WHERE colname The REGEXP, '[[: digit for:]] {}. 4';
denotes a search digit
is equivalent to [0-9] [0-9] [0 -9] [0-9]
Locator
Metacharacters | Explanation |
---|---|
^ | The beginning of the text |
$ | End of text |
[[:<:]] | Start times of word |
[[:>:]] | Words ending |
The SELECT * the FROM Table the WHERE colname The REGEXP, '^ [. 0-9 \]';
denotes a search beginning with a digit or character.
Note ^ in [] represented a negative sense