函数名称 | 描述 |
---|---|
current_data() |
当前日期 |
current_time() |
当前时间 |
current_timestamp() |
当前时间戳 |
date(datetime) |
返回datetume 参数的日期部分 |
date_add(date,interval d_value_type) |
在date 中添加日期时间 interval 后的数值单位可以是: year_minute second day |
date_sub(data,interval d_value_type) |
在date 中减去日期或时间 interval 后的数值单位可以是: year minute second day |
datadiff(date1,date2) |
两个日期的差,单位是天 |
now() |
当前日期时间 |
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-05-04 |
+----------------+
1 row in set (0.14 sec)
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 17:30:28 |
+----------------+
1 row in set (0.01 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-05-04 17:31:29 |
+---------------------+
1 row in set (0.03 sec)
mysql> select date_add('2023-5-1',interval 10 day);
+--------------------------------------+
| date_add('2023-5-1',interval 10 day) |
+--------------------------------------+
| 2023-05-11 |
+--------------------------------------+
1 row in set (0.01 sec)
--可以重命名
mysql> select date_add('2023-5-1',interval 10 day) as result;
+------------+
| result |
+------------+
| 2023-05-11 |
+------------+
1 row in set (0.01 sec)
--as可以省略
mysql> select date_add('2023-5-1',interval 10 day) result;
+------------+
| result |
+------------+
| 2023-05-11 |
+------------+
1 row in set (0.00 sec)
mysql> select date_sub('2023-5-1',interval 10 day);
+--------------------------------------+
| date_sub('2023-5-1',interval 10 day) |
+--------------------------------------+
| 2023-04-21 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2023-5-1',interval 10 day) as result;
+------------+
| result |
+------------+
| 2023-04-21 |
+------------+
1 row in set (0.00 sec)
mysql> select date_sub('2023-5-1',interval 10 day) result;
+------------+
| result |
+------------+
| 2023-04-21 |
+------------+
1 row in set (0.00 sec)
mysql> select datediff('2023-5-1','2023-6-1');
+---------------------------------+
| datediff('2023-5-1','2023-6-1') |
+---------------------------------+
| -31 |
+---------------------------------+
1 row in set (0.03 sec)
mysql> select datediff('2023-6-1','2023-5-1');
+---------------------------------+
| datediff('2023-6-1','2023-5-1') |
+---------------------------------+
| 31 |
+---------------------------------+
1 row in set (0.00 sec)
我们可能够发现是前面的日期减去后面的日期的时间差
mysql> create table tmp(
-> id int primary key auto_increment,
-> birthday date);
Query OK, 0 rows affected (0.29 sec)
mysql> insert into tmp(birthday) values (current_date());
Query OK, 1 row affected (0.04 sec)
mysql> select * from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 2023-05-04 |
+----+------------+
1 row in set (0.00 sec)
mysql> create table msg(
-> id int primary key auto_increment,
-> content varchar(20) not null,
-> sendtime datetime);
Query OK, 0 rows affected (0.23 sec)
mysql> insert into msg(content,sendtime) values ('hello1',now());
Query OK, 1 row affected (0.04 sec)
mysql> insert into msg(content,sendtime) values ('hello2',now());
Query OK, 1 row affected (0.05 sec)
mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime |
+----+---------+---------------------+
| 1 | hello1 | 2023-05-04 17:41:42 |
| 2 | hello2 | 2023-05-04 17:41:47 |
+----+---------+---------------------+
2 rows in set (0.01 sec)
select content,date(sendtime) from msg;
mysql> select content,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| hello1 | 2023-05-04 |
| hello2 | 2023-05-04 |
+---------+----------------+
2 rows in set (0.01 sec)
select * from msg where date_add(sendtime,interval 2 minute) > now();
理解:
------------------------------|-----------|-------------|------------------
初始时间 now() 初始时间+2min
charset(str) |
返回字符串字符集 |
---|---|
concat(string2 [,...]) |
连接字符串 |
instr(string,substring) |
返回substring 在string中出现的位置,没有返回0 |
ucase(string2) |
转换成大写 |
lcase(string2) |
转换成小写 |
left(string2,length) |
从string2 中的左边起取length 个字符 |
length(string) |
string 的长度 |
replace(str,search_str,replace_str) |
在str 中用replace_str 替换search_str |
strcmp(string1,string2) |
逐字符比较两字符的大小 |
substring(str,position [,length]) |
从str 的postion 开始,取length 个字符 |
ltrim(string) rtrim(string) trim(string) |
去除前空格或后空格 |
案例:
创建表
mysql> create table emp(
-> eid int primary key auto_increment,
-> ename varchar(20),
-> chinese int,
-> math int,
-> english int);
Query OK, 0 rows affected (0.23 sec)
插入数据若干:
mysql> insert into emp values(null,'孙悟空',80,90,77);
Query OK, 1 row affected (0.02 sec)
mysql> insert into emp values(null,'沙和尚',62,67,72);
Query OK, 1 row affected (0.03 sec)
emp
表的ename
列的字符集mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8 |
| utf8 |
+----------------+
2 rows in set (0.01 sec)
mysql> select concat(ename,'的语文成绩是',chinese,'分,数学',math,'分,英语',english,'分') as '分数' from emp;
+----------------------------------------------------------+
| 分数 |
+----------------------------------------------------------+
| 孙悟空的语文成绩是80分,数学90分,英语77分 |
| 沙和尚的语文成绩是62分,数学67分,英语72分 |
+----------------------------------------------------------+
2 rows in set (0.01 sec)
mysql> select length(ename),ename from emp;
+---------------+-----------+
| length(ename) | ename |
+---------------+-----------+
| 9 | 孙悟空 |
| 9 | 沙和尚 |
+---------------+-----------+
2 rows in set (0.01 sec)
注意:
length
函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数,如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文标识多个字节数(与字符集编码有关)
mysql> select replace(ename,'孙','sun'),ename from emp;
+----------------------------+-----------+
| replace(ename,'孙','sun') | ename |
+----------------------------+-----------+
| sun悟空 | 孙悟空 |
| 沙和尚 | 沙和尚 |
+----------------------------+-----------+
2 rows in set (0.01 sec)
ename
字段的第二个到第三个字符mysql> select substring(ename,2,2),ename from emp;
+----------------------+-----------+
| substring(ename,2,2) | ename |
+----------------------+-----------+
| 悟空 | 孙悟空 |
| 和尚 | 沙和尚 |
+----------------------+-----------+
2 rows in set (0.01 sec)
注意:这里一个汉字算一个字符,和一个字符占几个字节无关。
mysql> select * from emp;
+-----+-----------+---------+------+---------+
| eid | ename | chinese | math | english |
+-----+-----------+---------+------+---------+
| 1 | 孙悟空 | 80 | 90 | 77 |
| 2 | 沙和尚 | 62 | 67 | 72 |
| 3 | Lisa | 70 | 80 | 90 |
| 4 | Jack | 70 | 80 | 90 |
| 5 | Pony | 70 | 80 | 90 |
| 6 | Tony | 70 | 80 | 90 |
+-----+-----------+---------+------+---------+
6 rows in set (0.00 sec)
mysql> select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from emp;
+----------------------------------------------------------+
| concat(lcase(substring(ename, 1, 1)),substring(ename,2)) |
+----------------------------------------------------------+
| 孙悟空 |
| 沙和尚 |
| lisa |
| jack |
| pony |
| tony |
+----------------------------------------------------------+
6 rows in set (0.00 sec)
函数名称 | 描述 |
---|---|
abs(number) |
绝对值函数 |
bin(decimal_number) |
十进制转换二进制 |
hex(decimalNumber) |
转换成十六进制 |
conv(number,from_base,to_base) |
进制转换 |
ceiling(number) |
向上取整 |
floor(number) |
向下取整 |
format(number,decimal_places) |
格式化,保留小数位数 |
rand() |
返回随机浮点数,范围[0.0,1.0) |
mod(number,denominator) |
取模,求余 |
mysql> select abs(-100.2);
+-------------+
| abs(-100.2) |
+-------------+
| 100.2 |
+-------------+
1 row in set (0.01 sec)
mysql> select ceiling(23.4);
+---------------+
| ceiling(23.4) |
+---------------+
| 24 |
+---------------+
1 row in set (0.00 sec)
mysql> select ceiling(-23.4);
+----------------+
| ceiling(-23.4) |
+----------------+
| -23 |
+----------------+
1 row in set (0.00 sec)
mysql> select floor(23.4);
+-------------+
| floor(23.4) |
+-------------+
| 23 |
+-------------+
1 row in set (0.00 sec)
mysql> select floor(-23.4);
+--------------+
| floor(-23.4) |
+--------------+
| -24 |
+--------------+
1 row in set (0.00 sec)
mysql> select format(12.3456789,2);
+----------------------+
| format(12.3456789,2) |
+----------------------+
| 12.35 |
+----------------------+
1 row in set (0.00 sec)
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5188169232744881 |
+--------------------+
1 row in set (0.00 sec)
user()
查询当前用户mysql> select user();
+--------+
| user() |
+--------+
| Lxy@ |
+--------+
1 row in set (0.00 sec)
md5(str)
对一个字符串进行md5摘要,摘要后得到一个32位字符串mysql> select md5('123456');
+----------------------------------+
| md5('123456') |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set (0.00 sec)
database()
显示当前正在使用的数据库mysql> select database();
+----------------+
| database() |
+----------------+
| 104_db_lesson7 |
+----------------+
1 row in set (0.00 sec)
password()
函数,MySQL
数据库使用该函数对用户加密mysql> select password('Lxy');
+-------------------------------------------+
| password('Lxy') |
+-------------------------------------------+
| *48708BC40FD52498209A7778EDA8CB7BC76F8710 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
ifnull(val1,val2)
如果val1为null
,返回val2,否则返回val1
的值mysql> select ifnull('abc','123');
+---------------------+
| ifnull('abc','123') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.01 sec)
mysql> select ifnull(null,'123');
+--------------------+
| ifnull(null,'123') |
+--------------------+
| 123 |
+--------------------+
1 row in set (0.00 sec)
(本篇完)