Operators
Operators are reserved characters that perform an operation on one or more expressions. Use them for specifying conditions or conjunctions in SQL statements.
Raijin has the following types of operators:
Arithmetic operators
This table contains the list of arithmetic operators supported by Raijin.
Operator | Name | Operand type | Return type |
---|---|---|---|
|
Add |
INT, BIGINT, DOUBLE |
INT, BIGINT, DOUBLE |
|
Subtract |
INT, BIGINT, DOUBLE |
INT, BIGINT, DOUBLE |
|
Multiply |
INT, BIGINT, DOUBLE |
INT, BIGINT, DOUBLE |
|
Divide |
INT, BIGINT, DOUBLE |
INT, BIGINT, DOUBLE |
|
Modulus |
INT, BIGINT |
INT, BIGINT |
|
Negate |
INT, BIGINT, DOUBLE |
INT, BIGINT, DOUBLE |
Bitwise operators
The following table lists the bitwise operators supported by Raijin.
Operator | Name | Operand type | Return type |
---|---|---|---|
|
Binary AND |
BOOLEAN |
BOOLEAN |
|
Binary OR# |
INT, BIGINT# |
INT, BIGINT# |
|
Binary XOR# |
INT, BIGINT# |
INT, BIGINT# |
|
Binary NOT |
BOOLEAN |
INT, BIGINT |
|
Binary shift left |
INT, BIGINT |
INT, BIGINT |
|
Binary shift right# |
INT, BIGINT# |
INT, BIGINT# |
Comparison operators
The following table lists the comparison operators supported by Raijin.
Operator | Name | Operand type | Return type |
---|---|---|---|
|
Equal |
Any |
BOOLEAN |
|
Not equal |
Any |
BOOLEAN |
|
Not equal |
Any |
BOOLEAN |
|
Less than |
Any |
BOOLEAN |
|
Less than or equal |
Any |
BOOLEAN |
|
Greater than |
Any |
BOOLEAN |
|
Greater than or equal |
Any |
BOOLEAN |
Logical operators
The following table lists the logical operators supported by Raijin.
Operator | Name | Operand type | Return type |
---|---|---|---|
|
Logical AND |
BOOLEAN |
BOOLEAN |
|
Logical OR |
BOOLEAN |
BOOLEAN |
|
Logical XOR# |
BOOLEAN# |
BOOLEAN# |
|
Logical NOT |
BOOLEAN |
BOOLEAN |
|
Same as |
STRING |
STRING |
|
Same as |
STRING |
STRING |
|
Same as |
STRING |
STRING |
|
Same as |
STRING |
STRING |
Pattern-based search, case-sensitive |
STRING |
BOOLEAN |
|
Pattern-based search, case-insensitive |
STRING |
BOOLEAN |
|
Search in a list of values |
Any |
BOOLEAN |
LIKE
The LIKE
operator is used in a WHERE
clause to search for a pattern in a column. The LIKE
operator is case-sensitive.
Two wildcards are supported, %
represents zero or more characters, while _
represents exactly one character.
- Syntax
LIKE 'expr'
- Parameters
-
expr
-
Search pattern to match.
ILIKE
The ILIKE
operator is used in a WHERE
clause to search for a pattern in a column. The ILIKE
operator is not case-sensitive.
Two wildcards are supported, %
represents zero or more characters, while _
represents exactly one character.
- Syntax
ILIKE 'expr'
- Parameters
-
expr
-
Search pattern to match.
IN
The IN
operator is used to match an expression’s result against a list of values.
It is a shorthand for multiple OR
conditions.
- Syntax
expr IN (value_1, value_2, ..., value_n)
or
expr IN (subquery)
- Parameters
-
expr
-
Value to match.
value_1, value_2, …, value_n
-
List of values to match against. The
IN
operator returnstrue
if any value in the list matches the expression. subquery
-
A
SELECT
statement returning exactly one column. TheIN
operator returnstrue
if any value in the result set produced by the subquery matches the expression.
Special care should be taken when multiple IN
operators joined by an AND
specify different value types to match against.
This is because when the value lists contain variant data, the values are internally converted to a common data type for evaluation, depending on the value types specified.
Therefore, the result might be different from what you expect.
DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE tbl();
INSERT INTO tbl(a) VALUES (-5);
SELECT * FROM tbl WHERE a IN (-5) AND a IN ('-5', '-2');
-- Returns an empty result even though you might expect -5 to be found
Set operators
Raijin supports the following set operators:
UNION
The UNION
operator is used to combine the results of multiple SELECT
statements.
It requires that:
-
All
SELECT
statements have an equal number of columns -
The columns in each
SELECT
statement have similar data types -
The columns in each
SELECT
statement are specified in the same order
- Syntax
SELECT col_1, col_2, ..., col_n FROM table1
UNION
SELECT col_1, col_2, ..., col_n FROM table2
- Parameters
-
col_1, col_2, …, col_n
-
The list of columns to retrieve.
table
-
Table from where to retrieve the records.
DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE tbl1(a INTEGER, b INTEGER);
INSERT INTO tbl1(a, b) VALUES
(1, 10),
(2, 20),
(3, 30);
CREATE TABLE tbl2(a INTEGER, b INTEGER);
INSERT INTO tbl2(a, b) VALUES
(3, 30),
(4, 40),
(5, 50);
SELECT a, b FROM tbl1 UNION SELECT a, b FROM tbl2;
SELECT a, b, (a+b) AS sum FROM tbl1 UNION SELECT a, b, (a+b) FROM tbl2;
{"a":1,"b":10}
{"a":2,"b":20}
{"a":3,"b":30}
{"a":4,"b":40}
{"a":5,"b":50}
{}
{"a":1,"b":10,"sum":11}
{"a":2,"b":20,"sum":22}
{"a":3,"b":30,"sum":33}
{"a":4,"b":40,"sum":44}
{"a":5,"b":50,"sum":55}
UNION ALL
The UNION ALL
operator combines the results of multiple SELECT
statements, including duplicate rows.
- Syntax
SELECT col_1, col_2, ..., col_n FROM table1
UNION ALL
SELECT col_1, col_2, ..., col_n FROM table2
- Usage notes
-
See the UNION operator description.
DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE tbl1(a INTEGER, b INTEGER);
INSERT INTO tbl1(a, b) VALUES
(1, 10),
(2, 20),
(3, 30);
CREATE TABLE tbl2(a INTEGER, b INTEGER);
INSERT INTO tbl2(a, b) VALUES
(3, 30),
(4, 40),
(5, 50);
SELECT a, b FROM tbl1 UNION ALL SELECT a, b FROM tbl2;
SELECT a, b, (a+b) AS sum FROM tbl1 UNION ALL SELECT a, b, (a+b) FROM tbl2;
{"a":1,"b":10}
{"a":2,"b":20}
{"a":3,"b":30}
{"a":3,"b":30}
{"a":4,"b":40}
{"a":5,"b":50}
{}
{"a":1,"b":10,"sum":11}
{"a":2,"b":20,"sum":22}
{"a":3,"b":30,"sum":33}
{"a":3,"b":30,"sum":33}
{"a":4,"b":40,"sum":44}
{"a":5,"b":50,"sum":55}
String operators
The following table lists the string operators supported by Raijin.
Operator | Name | Operand type | Return type |
---|---|---|---|
|
Concatenate |
Any |
STRING |
Word-based search, case-sensitive |
STRING |
BOOLEAN |
|
Word-based search, case-insensitive |
STRING |
BOOLEAN |
MATCH AGAINST
Performs word-based search in selected columns, case-sensitive.
- Syntax
MATCH (col_name [, ...]) AGAINST (expr [, ...])
- Parameters
-
col_name
-
Column to be searched.
expr
-
Search string that specifies which words to search for.
- Usage notes
The table below shows some example searches.
Words in a search string not preceded by operator are optional, and the row is selected if any of the optional words is present in a column.
Words preceded by +
are mandatory, and they must be present in a row.
Words preceded by -
are prohibited, and they must not be present in a row.
Note that -
operator acts only to exclude rows that are otherwise matched by other search terms.
Thus, a search that contains only terms preceded by -
returns an empty result.
It does not return "all rows except those containing any of the excluded terms."
For +
and -
operators to have effect, they must be placed immediately before the word or open parentheses.
Otherwise, they are ignored.
For example, + word1
or - word1
are interpreted as ` word1` (meaning that word1
is optional).
Similarly, +word1 - (word2 word3)
is interpreted as +word1 (word2 word3)
.
Query | Description |
---|---|
SELECT * FROM tbl WHERE MATCH (*) AGAINST ('word1') |
Selects all rows that contain |
SELECT * FROM tbl WHERE MATCH (col1, col2) AGAINST ('word1') |
Select all rows that contain |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('word1') |
Selects all rows that contain |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('word1 word2') |
Selects all rows that contain either |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('+word1 +word2') |
Selects all rows that contain both |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('word1 -word2') |
Selects all rows that contain |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('word1 word2 +(word3 word4)') |
Selects all rows that contain |
SELECT * FROM tbl WHERE MATCH (col) AGAINST ('+word1 -(word2 word3)') |
Selects all rows that contain word1 and that neither contain |
Column is tokenized or split to words by considering everything that is not a letter or digit as a word boundary. That is, word is a sequence of letters and/or digits.
Infix syntax with the operators explicitly named and placed between operands is not supported, but it can be expressed using the supported syntax:
|
|
|
|
|
|
IMATCH AGAINST
Performs word-based search, case-insensitive.
- Syntax
IMATCH (col_name) AGAINST (expr)
- Parameters
-
col_name
-
Column to be searched.
expr
-
Search string that specifies which words to search for.
- Usage notes
-
See the MATCH AGAINST operator description.