目录
1.当前时间 now()/sysdate()/current_timestamp
1.绝对值 abs()
- select abs(11);#11
- select abs(-11);#11
2.向上取整 ceil()
- SELECT ceil(3.4)#4
- SELECT ceil(-3.4)#-3
3.向下取整 floor()
- SELECT FLOOR(2.56)#2
- SELECT FLOOR(-2.56)#-3
4.四舍五入 round()
- SELECT ROUND(3.14)#3
- # 四舍五入,保留相应的小数位
- SELECT ROUND(3.1415926,4)#3.1416
1.字节的长度 length()
- select length('abcd');#4
- select length('我');#3
- select length('😀');#4
- select length('abcd我😀');#11
2.字符的长度 char_length()
select char_length('abcd我😀');#6
3. 替换位置insert()
select insert('1234', 1, 2, 'a');#a34
4.替换相应的字符 replace()
select replace('1234', 3, 'a');#12a4
5.大写upper()
select upper('abc');#ABC
6. 小写lower()
select lower('abc');#abc
7.截取substr()
- # 指定开始截取位置
- select substr('abcd', 2);#bcd
- # 开始位置和截取的个数
- select substr('abcd', 2, 2);#bc
8.去除前后的空格trim()
-
- select trim(' abcd ');
9.去除左边空格 ltrim()
select ltrim(' abcd ');
10.去除右边的空格 rtrim()
select rtrim(' abcd ');
11.从左边开始截取 left()
select left('abcd', 2);#ab
12. 从右边开始截取right()
select right('abcd', 2);#cd
select replace(trim('a b c d'), ' ', '');
1.当前时间 now()/sysdate()/current_timestamp
- # 当前时间
- select now(); #2022-06-24 11:12:19
- # 当前时间
- select sysdate();#2022-06-24 11:12:31
- # 当前时间
- select current_timestamp;#2022-06-24 11:12:43
2.当前年份 year(now())
select year(now());#2022
3.当前月份month(now())
select month(now());#6
4.月份内天数 day(now())
select day(now());#24
5.本周是本年的第几周 week(now())
select week(now());#25
6.周几 dayofweek(now())
select dayofweek(now());#6 从星期天开始数
7.今年的第几天dayofyear(now())
select dayofyear(now());#175
1.增加一天
select date_add(now(), interval 1 day);#2022-06-25 11:15:37
2.增加一年
select date_add(now(), interval 1 year);#2023-06-24 11:15:49
3.减少一年
select date_sub(now(), interval 1 year);#2021-06-24 11:15:56
4.规范日期
select date_format(now(), '%Y年%m月%D天');#2022年06月24th天
案例:计算每个人的年龄
select sname,sage,year(now())-year(sage) + if(month(sage) > month(now()), -1, 0)from student;
5.求日期之间的差值(天数)
select datediff(now(),sage) from student;
6. 通过时间戳进行差值
select timestampdiff(month,sage,now()) from student;
7.加密函数(正常加密,加盐)
- select md5('12345');#827ccb0eea8a706c4c34a16891f84e7b
- select sha1('1');#356a192b7913b04c54574d18c28d46e6395428ab
- select sha2('1',256);#6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
8.union 联合(将数据联成一张表)
方法:
- select * from student
- union all
- select * from student;