目录
1.按位和 &
2.大于运算符 >
3.右移 >>
4.大于或等于运算符 >=
5.小于运算符 <
6.不等于运算符 <> !=
7.左移 <<
8.小于或等于运算符 <=
9.空安全等于运算符 <=>
编辑
10.模运算符 % MOD
11.乘法运算符 *
12.加法运算符 +
13.减法运算符 -
14.更改参数符号 -
15.评估路径后从JSON列返回值,相当于JSON_EXTRACT() ->
16.在计算路径和取消引用后从 JSON 列返回值 结果;等效于 JSON_UNQUOTE(JSON_EXTRACT()) ->>
19.除法运算符 /
20.分配值 :=
21.赋值(作为SET语句的一部分,或作为UPDATE语句中子句的一部分)SET =
22.相等运算符 =
23.按位异或 ^
24.返回绝对值 ABS()
25.返回反余弦函数 ACOS()
26.将时间值(间隔)添加到日期值 ADDDATE()
27.添加时间 ADDTIME()
28.使用 AES 加密 AES_ENCRYPT()
29.使用 AES 解密 AES_DECRYPT()
30.逻辑和 AND &&
31.抑制ONLY_FULL_GROUP_BY值拒绝 ANY_VALUE()
32.返回最左侧字符的数值 ASCII()
33.返回反正弦函数 ASIN()
34.返回反正切函数 ATAN()
35.返回两个参数的反正切 ATAN2(), ATAN()
36.返回参数的平均值 AVG()
37.重复执行表达式 BENCHMARK()
38.值是否在值范围内 BETWEEN...AND...
39.返回包含数字的二进制表示形式的字符串 BIN()
40.将二进制 UUID 转换为字符串 BIN_TO_UUID()
41.将字符串转换为二进制字符串 BINARY()
42.按位返回 AND BIT_AND()
43.返回设置的位数 BIT_COUNT()
44.返回参数的长度(以位为单位) BIT_LENGTH()
45.按位返回 OR BIT_OR()
46.按位返回异或 BIT_XOR()
47.返回不小于参数的最小整数值 CEIL()/CEILING()
48.返回传递的每个整数的字符 CHAR()
49.返回参数中的字符数 CHAR_LENGTH()
50.返回参数的字符集 CHARSET()
1.按位和 &
脚本
select 10 & 15,9 & 4 & 2;
分析
10 的补码为 1010,15 的补码为 1111,按位或运算之后,结果为 1111,即整数 15;9 的补码为 1001,4 的补码为 0100,2 的补码为 0010,按位或运算之后,结果为 111,即整数 15。
结果
2.大于运算符 >
脚本
select 1 > 2, 3 > 2;
结果
3.右移 >>
脚本
select 15 >> 3, 25 >> 5;
分析
向右位移的过程 15>>3
转换为二进制 15=1111
15的二进制数整体向右移动三个位置,移出的数丢弃得到 1
转换为十进制为1
技巧
15/2^3=1...7 取商1
n>>m 相当于 n/2的m次方取商
结果
4.大于或等于运算符 >=
脚本
select 1 >= 2, 2 >= 2;
结果
5.小于运算符 <
脚本
select 1 < 2, 3 < 2;
结果
6.不等于运算符 <> !=
代码
select 1 <> 2, 2 != 2;
结果
7.左移 <<
脚本
select 5 << 2, 2 << 5;
分析
向左位移过程 5<<2
转换为二进制 5=101
将5的二进制数整体向左移动两个位置空出来的补零得到 10100
转换为十进制为20
技巧
5*2^2=20
n<<m 相当于 n*2的m次方
结果
8.小于或等于运算符 <=
脚本
select 2 <= 3, 4 <= 3;
结果
9.空安全等于运算符 <=>
脚本
select null <=> null, 2 <=> 5, null = null, 1 <=> null;
结果
10.模运算符 % MOD
脚本
select 10 % 3, 5 mod 3;
结果
11.乘法运算符 *
脚本
select 2 * 3, 46512389652546516441523 * 6985412541754785426854.0;
分析
如果计算数字过大可以在后面加上点零(.0)转换为double类型
结果
12.加法运算符 +
脚本
select 2 + 3, 46512389652546516441523 + 6985412541754785426854;
结果
13.减法运算符 -
脚本
select 3 - 2, 46512389652546516441523 - 6985412541754785426854;
结果
14.更改参数符号 -
脚本
select -5, --6;
结果
15.评估路径后从JSON列返回值,相当于JSON_EXTRACT() ->
脚本
CREATE TABLE jemp(
c JSON,
g INT GENERATED ALWAYS AS (c -> '$.id'),
INDEX i (g)
);
INSERT INTO jemp (c) VALUES
('{"id": "1", "name": "Fred"}'), ('{"id": "2", "name": "Wilma"}'),
('{"id": "3", "name": "Barney"}'), ('{"id": "4", "name": "Betty"}');
SELECT c, c->'$.id', g
FROM jemp
WHERE c->'$.id' > 1
ORDER BY c->'$.id';
结果
16.在计算路径和取消引用后从 JSON 列返回值 结果;等效于 JSON_UNQUOTE(JSON_EXTRACT()) ->>
脚本
SELECT c ->> '$.name' AS name
FROM jemp
WHERE g > 2;
结果
19.除法运算符 /
脚本
select 3 / 2, 46512389652546516441523 / 6985412541754785426854;
结果
20.分配值 :=
脚本
select @ls, @ls := 2000000, @ls;
结果
21.赋值(作为SET语句的一部分,或作为UPDATE语句中子句的一部分)SET
=
脚本
create table test
(
id int primary key auto_increment,
money int
);
insert into test(money)
values (1000);
update test
set money = 2000
where id = 1;
select *
from test;
结果
22.相等运算符 =
脚本
select @lw;
select @lw := 2000000;
select @ls;
select @ls := 2000000;
select @ll;
select @ll = 100000;
select @lw = @ls, @ls = @ll;
结果
23.按位异或 ^
脚本
select 3 ^ 5;
分析
参加运算的两个数据,按二进制位进行位异或运算
例如:3^5
先将两个数据转化为二进制数,然后进行按位异或运算,只要位不同结果为1,不然结果为0;
即:11^101=110结果为6
异或运算的特殊用途:
1.使特定位翻转找一个数,对应X要翻转的各位,该数的对应位为1,其余位为零,此数与X对应位异或即可。
例:X=10101110,使X低4位翻转,用X ^0000 1111 = 1010 0001即可得到。
2.与0异或得到原值
例:X=10101110,用X^0=10101110
结果
24.返回绝对值 ABS()
脚本
select abs(-123);
结果
25.返回反余弦函数 ACOS()
脚本
select acos(0),acos(1);
ACOS(X) :返回X的反角余弦,即是余弦为X的值
结果
26.将时间值(间隔)添加到日期值 ADDDATE()
脚本
select now(), adddate(now(),interval 1 week);
结果
27.添加时间 ADDTIME()
脚本
SELECT ADDTIME('2022-12-31 23:59:59', '1 1:1:1');
结果
28.使用 AES 加密 AES_ENCRYPT()
脚本
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`passwd` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
insert into t1(passwd) values(hex(AES_ENCRYPT('123456','salt')));
insert into t1(passwd) values(hex(AES_ENCRYPT('abcdef','salt')));
结果:
29.使用 AES 解密 AES_DECRYPT()
脚本
SELECT id,AES_DECRYPT(unhex(passwd),'salt') from t1;
结果
30.逻辑和 AND &&
脚本
select 3 & 5,3 and 5;
分析
参加运算的两个数据,按二进制位进行位与运算.
例如:3&5
同为1结果为1,其它情况结果为0;
即:11&101=001结果为1
特别提醒:负数按补码形式参加按位与运算。
结果
31.抑制ONLY_FULL_GROUP_BY值拒绝 ANY_VALUE()
案例:
脚本
select any_value(sid), sname
from student group by Sname;
结果
32.返回最左侧字符的数值 ASCII()
脚本
select ascii(2);
分析:ascii对照表
结果
33.返回反正弦函数 ASIN()
脚本
select asin(0),asin(1);
分析
ASIN(X) :返回X的反角正弦,即是正弦为X的值
结果
34.返回反正切函数 ATAN()
脚本
select atan(pi()),atan(1);
结果
35.返回两个参数的反正切 ATAN2(), ATAN()
脚本
select atan(-2, 2), atan(pi(), 0)
结果
36.返回参数的平均值 AVG()
脚本
create database test1;
use test1;
create table a(
id int auto_increment primary key ,
score int);
insert into a (score) values (90),(85),(80),(70);
select avg(score)
from a ;
结果
37.重复执行表达式 BENCHMARK()
脚本
SELECT BENCHMARK(1000000,AES_ENCRYPT('hello','goodbye'));
分析
BENCHMARK(count ,expr) 函数 执行表达式
expr
重复计算次数。可能是 用于计时 MySQL 处理表达式的速度。这 结果值为 ,或者对于不适当的参数
结果
38.值是否在值范围内 BETWEEN...AND...
脚本
#用上面36个表的数据
select *
from a
where score between 85 and 95;
分析
expr BETWEEN min AND MAX:如果expr大于或等于min和expr小于或等于max(min <=expr and expr <=max)
结果
39.返回包含数字的二进制表示形式的字符串 BIN()
脚本
select bin('12');
分析
BIN(N):返回 N 的二进制值的字符串表示形式
结果
40.将二进制 UUID 转换为字符串 BIN_TO_UUID()
脚本
CREATE TABLE people
(
id BINARY(16) PRIMARY KEY,
name VARCHAR(255)
);
INSERT INTO people(id, name)
VALUES (UUID_TO_BIN(UUID()), 'John Doe'),
(UUID_TO_BIN(UUID()), 'Will Smith'),
(UUID_TO_BIN(UUID()), 'Mary Jane');
SELECT BIN_TO_UUID(id) id,
name
FROM people;
结果
41.将字符串转换为二进制字符串 BINARY()
脚本
select binary 'a' = 'a', binary 'a' = 'A', binary 'a' = 'a ';
分析
二进制运算符转换 二进制字符串(具有字符集和排序规则的字符串)的表达式。二进制的常见用途是强制字符 使用数字字节逐字节进行字符串比较 价值观,而不是逐个字符
结果
42.按位返回 AND BIT_AND()
脚本
CREATE TABLE t1
(
year YEAR(4),
month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL
);
INSERT INTO t1
VALUES (2000, 1, 1),
(2000, 1, 20),
(2000, 1, 30),
(2000, 2, 2),
(2000, 2, 23),
(2000, 2, 23);
select *
from t1;
SELECT year, month, BIT_and(day) AS days
FROM t1
GROUP BY year, month;
分析
二月份
2-> 00000010
23-> and 00010111
23-> and 00010111
= 00000010
十进制=2
一月份同理
结果
43.返回设置的位数 BIT_COUNT()
脚本
select BIT_COUNT(10);
分析
用来计算二进制数中包含1的个数。
因为10转成二进制是1010,所以该结果就是2。
结果
44.返回参数的长度(以位为单位) BIT_LENGTH()
脚本
select bit_length('李'),bit_length('li');
分析
命令 | 单位 | 一个汉字 | 一个数字or字母 | 示例 |
| 字符 | 1 | 1 |
|
| Byte | 3 | 1 |
|
| bit | 24 | 8 |
|
结果
45.按位返回 OR BIT_OR()
脚本
CREATE TABLE t1
(
year YEAR(4),
month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL
);
INSERT INTO t1
VALUES (2000, 1, 1),
(2000, 1, 20),
(2000, 1, 30),
(2000, 2, 2),
(2000, 2, 23),
(2000, 2, 23);
select *
from t1;
SELECT year, month, BIT_or(day) AS days
FROM t1
GROUP BY year, month;
分析
一月份
2 -> 00000001
20-> or 00010100
30-> or 00011110
= 00011111
十进制=31二月份同理
结果
46.按位返回异或 BIT_XOR()
脚本
CREATE TABLE t1
(
year YEAR(4),
month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL
);
INSERT INTO t1
VALUES (2000, 1, 1),
(2000, 1, 20),
(2000, 1, 30),
(2000, 2, 2),
(2000, 2, 23),
(2000, 2, 23);
select *
from t1;
SELECT year, month, BIT_XOR(day) AS days
FROM t1
GROUP BY year, month;
分析
一月份
2 -> 00000001
20-> xor 00010100
30-> xor 00011110
= 00001011
十进制=11二月份同理
结果
47.返回不小于参数的最小整数值 CEIL()/CEILING()
脚本
SELECT ceil(1.23),ceiling(2.01);
结果
48.返回传递的每个整数的字符 CHAR()
脚本
SELECT CHAR(77,121,83,81,76);
分析
CHAR(N,...[USING charset name]) 将每个参数 N 解释为整数并返回 由代码值给出的字符组成的字符串 的那些整数
结果
49.返回参数中的字符数 CHAR_LENGTH()
脚本
set @test='李';
select length(@test),char_length(@test);
分析
同44
结果
CHAR_LENGTH()与 CHARACTER_LENGTH()是同义词
50.返回参数的字符集 CHARSET()
脚本
select charset('li'),charset(convert('li' using latin1));
分析
更改字符集可以使用 convert(字符串 using 字符集名称)
结果
函数大全2
函数大全3
函数大全4
函数大全5
函数大全6
函数大全7
函数大全8