October 2002
Intermediate to advanced
1024 pages
27h 26m
English
You want to know whether strings are equal or unequal, or which one appears first in lexical order.
Use a comparison operator.
Strings are subject to the usual equality and inequality comparisons:
mysql> SELECT name, name = 'lead', name != 'lead' FROM metal;
+----------+---------------+----------------+
| name | name = 'lead' | name != 'lead' |
+----------+---------------+----------------+
| copper | 0 | 1 |
| gold | 0 | 1 |
| iron | 0 | 1 |
| lead | 1 | 0 |
| mercury | 0 | 1 |
| platinum | 0 | 1 |
| silver | 0 | 1 |
| tin | 0 | 1 |
+----------+---------------+----------------+You can also use relational operators such as
<, <=,
>=, and > to test strings
for lexical ordering:
mysql> SELECT name, name < 'lead', name > 'lead' FROM metal;
+----------+---------------+---------------+
| name | name < 'lead' | name > 'lead' |
+----------+---------------+---------------+
| copper | 1 | 0 |
| gold | 1 | 0 |
| iron | 1 | 0 |
| lead | 0 | 0 |
| mercury | 0 | 1 |
| platinum | 0 | 1 |
| silver | 0 | 1 |
| tin | 0 | 1 |
+----------+---------------+---------------+To find out whether a string lies within a given range of values (inclusive), you can combine two comparisons:
mysql> SELECT name, 'iron' <= name AND name <= 'platinum' FROM metal; +----------+---------------------------------------+ | name | 'iron' <= name AND name <= 'platinum' | +----------+---------------------------------------+ | copper | 0 | | gold | 0 | | iron ...