mysql> select
-> current_date();
select current_time();
select current_timestamp();
在日期上加上一段时间
select date_add("2022-03-20",interval 10 day);
select date_sub("2022-03-20",interval 10 day);
select datediff("2022-03-20","1-03-15");
create table msg(id int primary key auto_increment,test
varchar(32),sendtime datetime);
插入数据
其中now()就是获取当前时间,
显示所有留言信息,发布日期只显示日期,不用显示时间
select test,date(sendtime) from msg;
请查询在2分钟内发布的帖子
select *from msg where date_add(sendtime,interval 10 minute) >now();
2、字符串函数
获取msg表的test列的字符集
要求显示msg表中的信息,显示格式:“bianhai is xx 's xxxx xxxx”liiuyan l.
select concat ("bainhao is", id ,"'s",test,sendtime,"liuyan") from msg;
select length(test), test from msg;
注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数组算作一个字节,中文表示多个字节数(与字符集编码有关)
将msg表中所有test字段中有a的替换成’008’
select replace(test,"a","008"),test from msg;
select substring(test,2,2),test from msg;
user() 查询当前用户
md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
database()显示当前正在使用的数据库
ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值