& 按位与。例如:
mysql> select 15 & 1; +--------+ | 15 & 1 | +--------+ | 1 | +--------+ 1 row in set (0.02 sec)
| 按位或。例如:
mysql> select 15 | 1; +--------+ | 15 | 1 | +--------+ | 15 | +--------+ 1 row in set (0.01 sec)
^ 按位异或。例如:
mysql> select 15 ^ 1; +--------+ | 15 ^ 1 | +--------+ | 14 | +--------+ 1 row in set (0.02 sec)
~ 按位取反。例如:
mysql> select 15 & ~1; +---------+ | 15 & ~1 | +---------+ | 14 | +---------+ 1 row in set (0.04 sec)
>> 右移。例如:
mysql> select 1 << 2; +--------+ | 1 << 2 | +--------+ | 4 | +--------+ 1 row in set (0.03 sec)
<< 左移位。例如:
mysql> select 4 >> 2; +--------+ | 4 >> 2 | +--------+ | 1 | +--------+ 1 row in set (0.02 sec)
+ 加法运算符。例如:
mysql> select 8 + 1; +-------+ | 8 + 1 | +-------+ | 9 | +-------+ 1 row in set (0.04 sec)
- 减号运算符。例如:
mysql> select 8 - 1; +-------+ | 8 - 1 | +-------+ | 7 | +-------+ 1 row in set (0.04 sec)
* 乘法运算符。例如:
mysql> select 8 * 2; +-------+ | 8 * 2 | +-------+ | 16 | +-------+ 1 row in set (0.04 sec)
/ 除法运算符。例如:
mysql> select 7 / 2; +--------+ | 7 / 2 | +--------+ | 3.5000 | +--------+ 1 row in set (0.04 sec)
DIV 整数除法。例如:
mysql> select 8 / 7; +--------+ | 8 / 7 | +--------+ | 1.1429 | +--------+ 1 row in set (0.04 sec) mysql> select 8 div 7; +---------+ | 8 div 7 | +---------+ | 1 | +---------+ 1 row in set (0.03 sec)
> 大于运算符。例如:
mysql> select 8 > 7; +-------+ | 8 > 7 | +-------+ | 1 | +-------+ 1 row in set (0.01 sec)
>= 大于或等于运算符。例如:
mysql> select 8 >= 7; +--------+ | 8 >= 7 | +--------+ | 1 | +--------+ 1 row in set (0.02 sec)
< 小于运算符。例如:
mysql> select 8 < 7; +-------+ | 8 < 7 | +-------+ | 0 | +-------+ 1 row in set (0.02 sec)
<= 小于或等于运算符。例如:
mysql> select 8 <= 7; +--------+ | 8 <= 7 | +--------+ | 0 | +--------+ 1 row in set (0.02 sec)
= 相等运算符。例如:
mysql> select 8 = 7; +-------+ | 8 = 7 | +-------+ | 0 | +-------+ 1 row in set (0.02 sec)
<>, != 不等于运算符。例如:
mysql> select 8 != 7; +--------+ | 8 != 7 | +--------+ | 1 | +--------+ 1 row in set (0.02 sec) mysql> select 8 <> 7; +--------+ | 8 <> 7 | +--------+ | 1 | +--------+ 1 row in set (0.03 sec)
<=> NULL安全等于运算符。例如:
mysql> select 8 != null; +-----------+ | 8 != null | +-----------+ | NULL | +-----------+ 1 row in set (0.01 sec) mysql> select 8 <=> null; +------------+ | 8 <=> null | +------------+ | 0 | +------------+ 1 row in set (0.02 sec)
%, MOD 取余运算符。例如:
mysql> select 8 % 7; +-------+ | 8 % 7 | +-------+ | 1 | +-------+ 1 row in set (0.02 sec) mysql> select 8 mod 7; +---------+ | 8 mod 7 | +---------+ | 1 | +---------+ 1 row in set (0.02 sec)
AND, && 逻辑与。例如:
mysql> select true && false; +---------------+ | true && false | +---------------+ | 0 | +---------------+ 1 row in set (0.02 sec)
OR, || 逻辑或。例如:
mysql> select true || false; +---------------+ | true || false | +---------------+ | 1 | +---------------+ 1 row in set (0.03 sec)
XOR 逻辑异或。例如:
mysql> select true xor false; +----------------+ | true xor false | +----------------+ | 1 | +----------------+ 1 row in set (0.03 sec)
- 负数运算符。例如:
mysql> select -2; +----+ | -2 | +----+ | -2 | +----+ 1 row in set (0.01 sec)
-> 计算路径后 JSON 列的返回值,等效于 JSON_EXTRACT()。例如:
mysql> select * from t_demo; +----+-------------------------------+ | id | content | +----+-------------------------------+ | 1 | {"id": "3", "name": "Barney"} | | 2 | {"id": "4", "name": "Betty"} | | 3 | {"id": "2", "name": "Wilma"} | +----+-------------------------------+ 3 rows in set (0.02 sec) mysql> select content, content->"$.id", content->"$.name" from t_demo; +-------------------------------+-----------------+-------------------+ | content | content->"$.id" | content->"$.name" | +-------------------------------+-----------------+-------------------+ | {"id": "3", "name": "Barney"} | "3" | "Barney" | | {"id": "4", "name": "Betty"} | "4" | "Betty" | | {"id": "2", "name": "Wilma"} | "2" | "Wilma" | +-------------------------------+-----------------+-------------------+ 3 rows in set (0.02 sec)
->> 计算路径并取消对结果的引用后,返回JSON列的值;等效于JSON_UNQUOTE(JSON_EXTRACT())。例如:
mysql> select * from t_demo; +----+-------------------------------+ | id | content | +----+-------------------------------+ | 1 | {"id": "3", "name": "Barney"} | | 2 | {"id": "4", "name": "Betty"} | | 3 | {"id": "2", "name": "Wilma"} | +----+-------------------------------+ 3 rows in set (0.02 sec) mysql> select content, content->"$.id", content->"$.name" from t_demo; +-------------------------------+-----------------+-------------------+ | content | content->"$.id" | content->"$.name" | +-------------------------------+-----------------+-------------------+ | {"id": "3", "name": "Barney"} | "3" | "Barney" | | {"id": "4", "name": "Betty"} | "4" | "Betty" | | {"id": "2", "name": "Wilma"} | "2" | "Wilma" | +-------------------------------+-----------------+-------------------+ 3 rows in set (0.02 sec) mysql> select content, JSON_UNQUOTE(content->"$.id"), JSON_UNQUOTE(content->"$.name") from t_demo; +-------------------------------+-------------------------------+---------------------------------+ | content | JSON_UNQUOTE(content->"$.id") | JSON_UNQUOTE(content->"$.name") | +-------------------------------+-------------------------------+---------------------------------+ | {"id": "3", "name": "Barney"} | 3 | Barney | | {"id": "4", "name": "Betty"} | 4 | Betty | | {"id": "2", "name": "Wilma"} | 2 | Wilma | +-------------------------------+-------------------------------+---------------------------------+ 3 rows in set (0.03 sec) mysql> select content, content->>"$.id", content->>"$.name" from t_demo; +-------------------------------+------------------+--------------------+ | content | content->>"$.id" | content->>"$.name" | +-------------------------------+------------------+--------------------+ | {"id": "3", "name": "Barney"} | 3 | Barney | | {"id": "4", "name": "Betty"} | 4 | Betty | | {"id": "2", "name": "Wilma"} | 2 | Wilma | +-------------------------------+------------------+--------------------+ 3 rows in set (0.05 sec)
:= 指定一个值。例如:
mysql> SELECT @var1, @var2; +-------+-------+ | @var1 | @var2 | +-------+-------+ | NULL | NULL | +-------+-------+ 1 row in set (0.01 sec) mysql> SELECT @var1 := 1, @var2; +------------+-------+ | @var1 := 1 | @var2 | +------------+-------+ | 1 | NULL | +------------+-------+ 1 row in set (0.02 sec) mysql> SELECT @var1, @var2; +-------+-------+ | @var1 | @var2 | +-------+-------+ | 1 | NULL | +-------+-------+ 1 row in set (0.02 sec) mysql> SELECT @var1, @var2 := @var1; +-------+----------------+ | @var1 | @var2 := @var1 | +-------+----------------+ | 1 | 1 | +-------+----------------+ 1 row in set (0.02 sec) mysql> SELECT @var1:=COUNT(*) FROM t_demo; +-----------------+ | @var1:=COUNT(*) | +-----------------+ | 3 | +-----------------+ 1 row in set (0.02 sec) mysql> SELECT @var1; +-------+ | @var1 | +-------+ | 3 | +-------+ 1 row in set (0.03 sec)
= 赋值(作为 SET 语句的一部分,或作为 UPDATE 语句中 SET 子句的一部分)。例如:
mysql> set @my = 1000; Query OK, 0 rows affected (0.03 sec) mysql> select @my; +------+ | @my | +------+ | 1000 | +------+ 1 row in set (0.02 sec) mysql> update t_demo set content='{"id": "3", "name": "Barneys"}' where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
BETWEEN ... AND ... 值是否在值范围内。例如:
mysql> select * from t_demo where id between 1 and 2; +----+--------------------------------+ | id | content | +----+--------------------------------+ | 1 | {"id": "3", "name": "Barneys"} | | 2 | {"id": "4", "name": "Betty"} | +----+--------------------------------+ 2 rows in set (0.04 sec)
BINARY 将字符串转换为二进制字符串,常见用途是强制使用数字字节值而不是逐个字符逐字节地进行字符串比较。在 8.0.27 版本被废弃。例如:
mysql> select 'a' = 'A'; +-----------+ | 'a' = 'A' | +-----------+ | 1 | +-----------+ 1 row in set (0.02 sec) mysql> select binary 'a' = 'A'; +------------------+ | binary 'a' = 'A' | +------------------+ | 0 | +------------------+ 1 row in set (0.02 sec)
CASE CASE运算符,类似编程语言的 SWITCH 语句,例如:
mysql> select (case id when 1 then 'ONE' when 2 then 'TWO' else 'OTHERS' end) as id, content from t_demo; +--------+--------------------------------+ | id | content | +--------+--------------------------------+ | ONE | {"id": "3", "name": "Barneys"} | | TWO | {"id": "4", "name": "Betty"} | | OTHERS | {"id": "2", "name": "Wilma"} | +--------+--------------------------------+ 3 rows in set (0.03 sec)
IN() 值是否在一组值内,例如:
mysql> select * from t_demo where id in(1,2); +----+--------------------------------+ | id | content | +----+--------------------------------+ | 1 | {"id": "3", "name": "Barneys"} | | 2 | {"id": "4", "name": "Betty"} | +----+--------------------------------+ 2 rows in set (0.05 sec)
IS 根据布尔值测试值,例如:
mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN; +-----------+------------+-----------------+ | 1 IS TRUE | 0 IS FALSE | NULL IS UNKNOWN | +-----------+------------+-----------------+ | 1 | 1 | 1 | +-----------+------------+-----------------+ 1 row in set (0.02 sec)
IS NOT 根据布尔值测试值,例如:
mysql> SELECT 1 IS NOT TRUE, 0 IS NOT FALSE, NULL IS NOT UNKNOWN; +---------------+----------------+---------------------+ | 1 IS NOT TRUE | 0 IS NOT FALSE | NULL IS NOT UNKNOWN | +---------------+----------------+---------------------+ | 0 | 0 | 0 | +---------------+----------------+---------------------+ 1 row in set (0.02 sec)
IS NOT NULL NOT NULL值测试,例如:
mysql> select * from t_demo where id is not null; +----+--------------------------------+ | id | content | +----+--------------------------------+ | 1 | {"id": "3", "name": "Barneys"} | | 2 | {"id": "4", "name": "Betty"} | | 3 | {"id": "2", "name": "Wilma"} | +----+--------------------------------+ 3 rows in set (0.02 sec)
IS NULL NULL值测试,例如:
mysql> select * from t_demo where content is null; +----+---------+ | id | content | +----+---------+ | 4 | NULL | +----+---------+ 1 row in set (0.03 sec)
LIKE 简单模式匹配,例如:
mysql> select * from t_demo where content like '%B%'; +----+--------------------------------+ | id | content | +----+--------------------------------+ | 1 | {"id": "3", "name": "Barneys"} | | 2 | {"id": "4", "name": "Betty"} | +----+--------------------------------+ 2 rows in set (0.04 sec)
MEMBER OF() 来自 8.0.17 版本,如果第一个操作数与作为第二个操作数传递的 JSON 数组的任何元素匹配,则返回 true(1),否则返回 false(0)。例如:
mysql> SELECT 17 MEMBER OF('[23, "abc", 17, "ab", 10]'); +-------------------------------------------+ | 17 MEMBER OF('[23, "abc", 17, "ab", 10]') | +-------------------------------------------+ | 1 | +-------------------------------------------+ 1 row in set (0.01 sec)
NOT, ! 否定值,例如:
mysql> select * from t_demo where id != 2; +----+--------------------------------+ | id | content | +----+--------------------------------+ | 1 | {"id": "3", "name": "Barneys"} | | 3 | {"id": "2", "name": "Wilma"} | | 4 | NULL | +----+--------------------------------+ 3 rows in set (0.02 sec)
NOT BETWEEN ... AND ... 值是否不在值范围内,例如:
mysql> select * from t_demo where id not between 1 and 2; +----+------------------------------+ | id | content | +----+------------------------------+ | 3 | {"id": "2", "name": "Wilma"} | | 4 | NULL | +----+------------------------------+ 2 rows in set (0.03 sec)
NOT IN() 值是否不在一组值内,例如:
mysql> select * from t_demo where id not in(1,2); +----+------------------------------+ | id | content | +----+------------------------------+ | 3 | {"id": "2", "name": "Wilma"} | | 4 | NULL | +----+------------------------------+ 2 rows in set (0.03 sec)
NOT LIKE 简单模式匹配的否定,例如:
mysql> select * from t_demo where content not like '%B%'; +----+------------------------------+ | id | content | +----+------------------------------+ | 3 | {"id": "2", "name": "Wilma"} | +----+------------------------------+ 1 row in set (0.03 sec)
REGEXP 字符串是否与正则表达式匹配,例如:
mysql> SELECT 'a' REGEXP '^[a-d]'; +---------------------+ | 'a' REGEXP '^[a-d]' | +---------------------+ | 1 | +---------------------+ 1 row in set (0.28 sec)
RLIKE 字符串是否与正则表达式匹配,例如:
mysql> SELECT 'a' RLIKE '^[a-d]'; +--------------------+ | 'a' RLIKE '^[a-d]' | +--------------------+ | 1 | +--------------------+ 1 row in set (0.02 sec)
NOT REGEXP REGEXP 的否定,例如:
mysql> SELECT 'a' NOT REGEXP '^[a-d]'; +-------------------------+ | 'a' NOT REGEXP '^[a-d]' | +-------------------------+ | 0 | +-------------------------+ 1 row in set (0.02 sec)
SOUNDS LIKE 比较声音,与 SOUNDEX(expr1) = SOUNDEX(expr2) 相同,SOUNDEX 返回由四个字符组成的代码 (SOUNDEX) 以评估两个字符串在发音时的相似性。例如:
mysql> select SOUNDEX('Hello'); +------------------+ | SOUNDEX('Hello') | +------------------+ | H400 | +------------------+ 1 row in set (0.03 sec) mysql> select SOUNDEX('Quadratically'); +--------------------------+ | SOUNDEX('Quadratically') | +--------------------------+ | Q36324 | +--------------------------+ 1 row in set (0.04 sec) mysql> select 'Hello' SOUNDS LIKE 'Quadratically'; +-------------------------------------+ | 'Hello' SOUNDS LIKE 'Quadratically' | +-------------------------------------+ | 0 | +-------------------------------------+ 1 row in set (0.03 sec)