- MySQL 5.1 Reference Manual :: 11 Functions and Operators :: 11.2 Operators :: 11.2.4 Logical Operators
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
Table 11.4. Logical Operators
Name Description AND
,&&
Logical AND NOT
,!
Negates value ||
,OR
Logical OR XOR
Logical XOR In SQL, all logical operators evaluate to
TRUE
,FALSE
, orNULL
(UNKNOWN
). In MySQL, these are implemented as 1 (TRUE
), 0 (FALSE
), andNULL
. Most of this is common to different SQL database servers, although some servers may return any nonzero value forTRUE
.Note that MySQL evaluates any nonzero or non-
NULL
value toTRUE
. For example, the following statements all assess toTRUE
:mysql>
SELECT 10 IS TRUE;
-> 1 mysql>SELECT -10 IS TRUE;
-> 1 mysql>SELECT 'string' IS NOT NULL;
-> 1-
Logical NOT. Evaluates to
1
if the operand is0
, to0
if the operand is nonzero, andNOT NULL
returnsNULL
.mysql>
SELECT NOT 10;
-> 0 mysql>SELECT NOT 0;
-> 1 mysql>SELECT NOT NULL;
-> NULL mysql>SELECT ! (1+1);
-> 0 mysql>SELECT ! 1+1;
-> 1The last example produces
1
because the expression evaluates the same way as(!1)+1
. -
Logical AND. Evaluates to
1
if all operands are nonzero and notNULL
, to0
if one or more operands are0
, otherwiseNULL
is returned.mysql>
SELECT 1 && 1;
-> 1 mysql>SELECT 1 && 0;
-> 0 mysql>SELECT 1 && NULL;
-> NULL mysql>SELECT 0 && NULL;
-> 0 mysql>SELECT NULL && 0;
-> 0 -
Logical OR. When both operands are non-
NULL
, the result is1
if any operand is nonzero, and0
otherwise. With aNULL
operand, the result is1
if the other operand is nonzero, andNULL
otherwise. If both operands areNULL
, the result isNULL
.mysql>
SELECT 1 || 1;
-> 1 mysql>SELECT 1 || 0;
-> 1 mysql>SELECT 0 || 0;
-> 0 mysql>SELECT 0 || NULL;
-> NULL mysql>SELECT 1 || NULL;
-> 1 -
Logical XOR. Returns
NULL
if either operand isNULL
. For non-NULL
operands, evaluates to1
if an odd number of operands is nonzero, otherwise0
is returned.mysql>
SELECT 1 XOR 1;
-> 0 mysql>SELECT 1 XOR 0;
-> 1 mysql>SELECT 1 XOR NULL;
-> NULL mysql>SELECT 1 XOR 1 XOR 1;
-> 1a XOR b
is mathematically equal to(a AND (NOT b)) OR ((NOT a) and b)
.