SQL reference (MySQL and MariaDB dialects)

String operators

LIKE, REGEXP and RLIKE, COLLATE and BINARY: patterns, case, wildcards and what is not supported yet.

Syntax#

SQL
expr [NOT] LIKE pattern
expr [NOT] REGEXP pattern
expr [NOT] RLIKE pattern
expr COLLATE collation_name
BINARY expr

LIKE#

LIKE compares a text with a pattern. In the pattern % matches any number of characters (also none), _ matches exactly one character, and \ makes the next character literal.

  • The match ignores case: 'Alice' LIKE 'a%' is 1, 'Alice' LIKE '_lic_' is 1, 'Alice' LIKE 'b%' is 0.
  • \% and \_ match a percent sign and an underscore, but in a string literal Raft DB drops a backslash that does not make an escape sequence, so write the backslash twice in the literal: '100%' LIKE '100\\%' is 1.
  • LIKE ... ESCAPE 'c' is not supported yet (error: not supported).
  • A number is converted to its text. NULL on either side gives NULL.
  • NOT LIKE negates.
SQL
SELECT w FROM words WHERE w LIKE 'APP%';       -- finds Apple and apple2
SELECT COUNT(*) FROM words WHERE n > 1 AND (w LIKE 'a%' OR w LIKE 'b%');

REGEXP, RLIKE, REGEXP_LIKE#

expr REGEXP pattern returns 1 if the pattern matches anywhere in expr. RLIKE is a synonym, REGEXP_LIKE(expr, pattern) is the function form, NOT REGEXP negates. Matching ignores case. The pattern syntax is on Regular expression functions. An invalid pattern is error 3692 (Illegal argument to a regular expression).

SQL
SELECT 'abab' REGEXP '^(ab)\\1$', 'foobar' REGEXP 'foo(?=bar)', 'xbar' REGEXP '(?<=x)bar';
SELECT 'Hello World' REGEXP 'world';        -- 1

COLLATE and BINARY#

  • expr COLLATE name with a case-insensitive collation changes nothing. A binary or case-sensitive collation (utf8mb4_bin, a name ending _cs, binary) is refused as not supported.
  • BINARY expr is accepted and does not change the comparison. See Collation and case sensitivity.

Concatenation#

There is no concatenation operator: || is OR. Use CONCAT(a, b) or CONCAT_WS(sep, a, b) (see String functions).

Not supported yet#

LIKE ... ESCAPE, SOUNDS LIKE, MATCH ... AGAINST (full-text), case-sensitive LIKE or REGEXP (BINARY, utf8mb4_bin), the match_type argument, regex flags such as (?i).

See also#