home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeManaging and Using MySQLSearch this book

Chapter 17. Operators and Functions

Like any other programming language, SQL carries among its core elements operators and named procedures. This reference lists all those operators and functions and explains how they evaluate into useful expressions.

17.1. Operators

MySQL offers three kinds of operators: arithmetic, comparison, and logical.

17.1.3. Comparison Operators

Comparison operators compare values and return 1 if the comparison is true and 0 otherwise. Except for the <==> operator, NULL values cause a comparison operator to evaluate to NULL.

<> or !=
Match rows if the two values are not equal.

<=
Match rows if the left value is less than or equal to the right value.

<
Match rows if the left value is less than the right value.

>=
Match rows if the left value is greater than or equal to the right value.

>
Match rows if the left value is greater than the right value.

value BETWEEN value1 AND value2
Match rows if value is between value1 and value2, or equal to one of them.

value IN (value1,value2,...)
Match rows if value is among the values listed.

value NOT IN (value1, value2,...)
Match rows if value is not among the values listed.

value1 LIKE value2
Compares value1 to value2 and matches the rows if they match. The righthand value can contain the wildcard '%', which matches any number of characters (including 0), and '_', which matches exactly one character. This is probably the single most used comparison in SQL. Its most common use is comparing a field value with a literal containing a wildcard (e.g., SELECT name FROM people WHERE name LIKE 'B%').

value1 NOT LIKE value2
Compares value1 to value2 and matches the rows if they differ. This is identical to NOT (value1 LIKE value2).

value1 REGEXP/RLIKE value2
Compares value1 to value2 using the extended regular expression syntax and matches the rows if the two values match. The righthand value can contain full Unix regular expression wildcards and constructs (e.g., SELECT name FROM people WHERE name RLIKE '^B.*').

value1 NOT REGEXP value2
Compares value1 to value2 using the extended regular expression syntax and matches the rows if they differ. This is identical to NOT (value1 REXEXP value2).



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.