功能:将多个字符串连接在一起
例:
select concat('aa','bb','cc') as result ;
select concat('aa','bb','cc') from dual;
select concat ('编号为',empno,'的员工,姓名为',ename) from emp;
注:dual表是MySQL提供的一个虚拟表,主要是为了满足select…from…的语法习惯,一般测试时使用,无实际意义
功能:将字符串s中的字符转换为小写字母
select lower('HELLO WORLD') from dual;
功能:将字符串s中的字符转换为大写字母
select upper('hello world') from dual;
功能:获取字符串s的长度,测试s中字符串的长度
select length('hello world') from dual;
功能:将字符串s中的内容反转
select reverse('hello world') from dual;
功能:去掉字符串两边的空格 还有ltrim(s)和rtrim(s)表示去掉字符串左边和右边的空格
select trim(' hello world ') from dual;
功能:将字符串s中的s1替换为s2
select replace('hello world','o','xixihaha') from dual;
功能:在字符串s的左边使用s1进行填充,直到长度为len为止
select lpad('hello',8,'#') from dual;
功能:在字符串s的右边使用s1进行填充,直到长度为len为止
功能:将字符串s重复n次后返回
select repeat('book',3) from dual;
功能:从字符串的第i个位置开始,取len个字符
select substr('my book',4,3) from dual;
功能:返回大于n的最小整数
select ceil(10.1)from dual;
功能:返回小于n的最大整数
select floor(10.1)from dual;
功能:对n进行四舍五入,保留y位小数
select round(3.1415,3)from dual;
功能:对n保留y位小数,不进行四舍五入
select truncate(3.14159,3)from dual;
功能:返回0-1之间的随机数,ps:该函数没有参数(无参,哑参)
select rand()from dual;
now()
功能:返回当前日期时间
**select now()**from dual;
curdate()
功能:返回当前日期
select curdate()from fual;
功能:返回当前时间
select curtime()from dual;
功能:返回指定日期的年
select year(‘2025-4-22’)from dual;
month(日期)
功能:返回指定日期的月
select month (‘2025-4-22’)from dual;
功能:返回指定日期的日
select day(“2025-4-22”)from dual;
功能:返回两个日期时间之间相隔的时间戳,单位是由间隔类型来指定的,interval间隔类型:year month dat hour minute second
select timestampdiff(day,'2003-1-10','2025-4-22')from dual;
功能:格式化日期
select date_format(now(),'%Y年%m月%d日 %H:%i:%s')from dual;
PS:
%Y表示四位数字的年
%m表示两位数字的月
%d表示两位数字的日
%H表示两位数字的小时,24小时值
%i表示两位数字的分钟
%s表示两位数字的秒数
p228
表2-2-4
功能:如果条件为真,执行v1,如果非真,执行v2
select if(5>2,‘yes’,‘no’);
功能:如果v1不是空值,返回v1,否则返回v2
ifnull(null,‘0’)from dual;
功能:如果f1为真,返回v1,如果f2真,返回v2,否则返回v
select case when 5>2 then 'yes' end from dual;
select case when 5>2 then 'yes' else 'no' end from dual;
select case when 5<2 then 'one' when 6>4 then 'two' else 'three' end from dual;
功能:返回当前操作的数据库
select database() from dual;
功能:返回当前登录用户
select user();
功能:返回MySQL服务器的版本
select version();