MySQL 常用数据类型
MySQL 常用函数
单行函数
字符串函数
#length() 查字符串长度
select length("11") as length; # 2
#拼接函数
select concat("test", "_", "pp"); # test_pp
#LOWER, UPPER
select lower("WWWSSS"); # wwwsss
select upper("wwwssss"); # WWWSSSS
#substr | substring 两个的意义是一样的.
select substr('test', 3); # 返回后面的所有字节, 下标从1开始 st
select substr('test', 1, 3); # 截取从1开始 获取3个字符 tes
#insert
select insert("insert", 1, 2, "3333"); # 将从1开始长度为2的字符串替换为3333
#trim 去除空格
select TRIM(" a ");
#去除前面|后面是 aa 的字符
select TRIM("aa" FROM " aaaaaatest三十岁时aaaaassssaaaaa ");
#replace: 替换函数.
select replace("Android_Test_Android", "_", "#");
#reverse: 逆转字符.
select reverse("Android_TEST_Java"); #avaJ_TSET_diordnA
数值函数
select abs(-10); #绝对值: 10
select sqrt(10000); #开方: 100
select mod(10, 3); # 取余数 1
select sign(-10); # 1 | -1
select pow(10000, 0.5); # 多少次方
# 三角函数
select sin(100);
select cos(100);
select tan(100);
select asin(100);
select acos(100);
select atan(100);
select round(1.55); #2 四舍五入
select round(1.55, 1); #1.6 四舍五入, 保留1位小数点
select ceil(12.1); #13 向上取整
select floor(14.5); #14 向下取整
日期函数
# 获得时间
select curdate();
select curtime();
select now();
select unix_timestamp();
select unix_timestamp("2019-12-14 20:23:24");
select from_unixtime(unix_timestamp());
# 从时间获得属性
#时间格式: t = "2019-12-13 11:23:26"
select year(t), month(t), day(t), hout(t), minute(t), second(t);
select monthname(t); # December
select dayname(t), dayofyear(t), dayofweek(t); # Friday, 346, 6
select week(t), weekday(t), weekofyear(t); # 49, 4, 50
# 操作时间
select time_to_sec(t); # 41006
select subtime(t, "5"); #从日期中减去间隔时间, 返回结果, 间隔时间正负都可, 2019-12-13 11:23:21
流程控制函数
# if函数: if else 的效果, 可以多级 if() 判断. 结合 count(if()), sum(if()) 函数可以实现过滤统计效果.
select if(10 < 5, '大', '小');
# case when, 实现和 if 函数类型效果.
## 1. 范围判断
select
case
when 10 > 5 then '大'
else then '小'
end
##2. 等值判断.
select salary,
CASE department_id
WHEN 30 THEN salary * 2
WHEN 40 THEN salary * 3
ELSE salary
END
FROM employees;
分组函数(聚合函数)
1、sum、avg一般用于处理数值型 max、min、count可以处理任何类型 2、分组函数默认都忽略null值 3、可以和distinct搭配实现去重的运算
4、count函数的单独介绍, 一般使用count(*)用作统计行数
5、和分组函数一同查询的字段要求是group by后的字段
select sum(salary) from employees;
select avg(salary) from employees;
select min(salary) from employees;
select max(salary) from employees;
select count(salary) from employees;
select group_concat(a, b) c from employees;
Mysql 运算符
1. <=> 与 =, is null 区别
<=> 和 =的区别: <=>可以判断值为 null, =不能判断值为null 的情况
<=>和 IS NULL 的区别:
-
IS NULL只能判断值为null, 可读性高
-
<=>能判断值为null, 也可以判断其他值, 可读性低
2. between 用法
- 用在数值取值方面: 两个值之间, 1 < x < 10
- 用在日期取值方面: 两个日期之间: 1996-01-01 < x < 2023-01-01
3. like 模糊查询
常见的正则匹配操作.
%a以a结尾的数据
a%以a开头的数据
%a%含有a的数据
_a_三位且中间字母是a的
_a两位且结尾字母是a的
_a_两位且开头字母是a的
mysql> select * from admin where username like '%l%';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 2 | lyt | 6666 |
+----+----------+----------+
4. concat 连接函数使用方式
- concat 函数会将所有参数当做字符串, 直接拼接起来
- concat 碰到 null, 结果直接为 null
注意点: MySQL 中, 字符需要写上引号, 不然会报错: 1054 - Unknown column 'abc' in 'field list'
mysql> select concat(1, 2) as concat;
+--------+
| concat |
+--------+
| 12 |
+--------+
mysql> select concat('abc', 'defg') as concat;
+---------+
| concat |
+---------+
| abcdefg |
+---------+
mysql> select concat('abc', 'defg', null) as concat;
+--------+
| concat |
+--------+
| NULL |
+--------+
3. 使用 + 号 和 concat 区别
+会将前后两个值相加+会尝试将字符串转换为 number 类型值, 如果转换失败, 则值为0+前后有 null, 则结果直接为 NULL.
mysql> select 2 + 5 as sum; # 7
mysql> select '2' + 5 as sum; # 7
mysql> select 'a' + 5 as sum; # 5
mysql> select 'a' + 'b' as sum; # 0
mysql> select 5 + null as sum; # null