Database Reference
In-Depth Information
Tip
More than Two
OR
Conditions More than two
OR
conditions may be specified. For
example,
'1000|2000|3000'
would match
1000
or
2000
or
3000
.
.
matches any single character. But what if you wanted to match only specific
characters? You can do this by specifying a set of characters enclosed within
[
and
]
, as seen here:
▼
Input
SELECT prod_name
FROM products
WHERE prod_name REGEXP '[123] Ton'
ORDER BY prod_name;
▼
Output
+-------------+
| prod_name |
+-------------+
| 1 ton anvil |
| 2 ton anvil |
+-------------+
▼
Analysis
Here the regular expression
[123] Ton
was used.
[123]
defines a set of char-
acters, and here it means
match
1
or
2
or
3
, so both
1 ton
and
2 ton
matched
and were returned (there was no
3 ton
).
As you have just seen,
[]
is another form of
OR
statement. In fact, the regular
expression
[123] Ton
is shorthand for
[1|2|3] Ton
, which also would have
worked. But the
[]
characters are needed to define what the
OR
statement is
looking for. To better understand this, look at the next example:
▼
Input
SELECT prod_name
FROM products
WHERE prod_name REGEXP '1|2|3 Ton'
ORDER BY prod_name;