Updated on 2025-10-23 GMT+08:00

Logical Operators

M-compatible databases support four logical operators: AND (&&), OR (||), NOT (!), and XOR. For details, see Table 1. There are three types of operation results: TRUE, FALSE, and NULL.

Table 1 Logical operator details

Operator

Operand

Description

NOT/!

Unary

NOT.

AND/&&

Binary

AND.

OR/||

Binary

OR.

XOR

Binary

XOR.

NOT and AND, OR, and XOR list the calculation rules, where a and b represent logical expressions.

NOT

If the operand is TRUE, the calculation result is FALSE. If the operand is FALSE, the calculation result is TRUE.

Value range: Boolean

The following lists calculation rules.

Table 2 NOT calculation rules (calculation based on TRUE, FALSE, and NULL)

a

NOT a

TRUE

FALSE

FALSE

TRUE

NULL

NULL

The operators ! and NOT have the same function, but the priority of the operator ! is higher than that of NOT.

Examples:

m_db=# SELECT NOT TRUE;
 ?column? 
----------
 f
(1 row)

m_db=# SELECT NOT FALSE;
 ?column? 
----------
 t
(1 row)

m_db=# SELECT NOT NULL;
 ?column? 
----------
 
(1 row)

AND, OR, and XOR

The calculation logic of AND, OR, and XOR is listed in the following table.

Table 3 Operation rules of AND, OR, and XOR (calculation based on TRUE, FALSE, and NULL)

a

b

Result of a AND b

Result of a OR b

Result of a XOR b

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

TRUE

TRUE

TRUE

NULL

NULL

TRUE

NULL

FALSE

FALSE

FALSE

FALSE

FALSE

FALSE

NULL

FALSE

NULL

NULL

NULL

NULL

NULL

NULL

NULL

The operators AND, XOR, and OR are commutative, that is, you can switch the left and right operand without affecting the result.

Examples:

m_db=# SELECT TRUE AND TRUE;
 ?column? 
----------
 t
(1 row)

m_db=# SELECT TRUE OR FALSE;
 ?column? 
----------
 t
(1 row)

m_db=# SELECT TRUE XOR NULL;
 ?column? 
----------
 
(1 row)

Specification Constraints of Logical Operators

Logical operations support all data types supported by the current M-compatible database. During data operations between different major types, the data is promoted to the major type first, and then the calculation is performed based on whether the result of the promoted major type is 0. The following table lists the promotion rules.

Table 4 Logical operation type classification

Operator

Left and Right Input Parameter Type

Classification Type

XOR

All types

BIGINT

AND, OR, and NOT

TINYINT, SMALLINT, MEDIUMINT, INT/INTEGER, BIGINT, TINYINT UNSIGNED, SMALLINT UNSIGNED, MEDIUMINT UNSIGNED, INT/INTEGER UNSIGNED, BIGINT UNSIGNED, BIT, YEAR, and BOOL/LBOOLEAN

BIGINT

Others

DOUBLE