目录
一、日期时间函数
1.1 简单使用
select current_date();
select current_time();
select current_timestamp();
select date(current_timestamp());
select date_add(current_timestamp(),interval 10 day);
select date_add(current_timestamp(),interval 10 minute);
select date_sub(current_timestamp(),interval 10 second);
select date_sub(current_timestamp(),interval 10 year);
select datediff('2000-1-1',current_date());
select datediff('2050-1-1',current_date());
`
select now();
1.2 案例实操
创建一张表,记录生日。
create table if not exists birthday(
id int primary key auto_increment,
birthday date not null
);
二、字符串函数
2.1 简单使用
select charset('LOVE');
select charset('中国');
select concat('LOVE',' ','中国');
select instr('I love you,China!','China');
select instr('I love you,China!','me');
select ucase('I love you,China!');
select lcase('I love you,China!');
select left('I love you,China!',6);
select right('I love you,China!',6);
select length('I love you,China!');
select length('5201314');
select replace('I love you,China!','I','We');
select strcmp('Love','LOVE');
select strcmp('Love','I');
select substring('I love you,China!',3,8);
select substring('I love you,China!',11,8);
select ltrim(' Love ') res ;
select rtrim(' Love ') res ;
select trim(' Love ') res ;
2.2 案例实践
DROP database IF EXISTS `scott`;
CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `scott`;
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`empno` int(6) unsigned zerofill NOT NULL COMMENT '雇员编号',
`ename` varchar(10) DEFAULT NULL COMMENT '雇员姓名',
`job` varchar(9) DEFAULT NULL COMMENT '雇员职位',
`mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇员领导编号',
`hiredate` datetime DEFAULT NULL COMMENT '雇佣时间',
`sal` decimal(7,2) DEFAULT NULL COMMENT '工资月薪',
`comm` decimal(7,2) DEFAULT NULL COMMENT '奖金',
`deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部门编号'
);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'TURNER', 'SALESMAN', 7698,'1981-09-08', 1500, 0, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
2.2.1 获取emp表的ename列的字符集
select ename,charset(ename) from emp;
2.2.2 要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
select concat(name,'的语文是',chinese,',数学是',math,',英语是',english) from exam_result;
2.2.3 求exam_result表中学生姓名占用的字节数
select name,length(name) from exam_result;
注意:
length函数返回字符串长度,以字节为单位,而非字符,中文一个字符就是多个字节,与字符集编码有关。
2.2.4 将EMP表中所有名字中有S的替换成’上海’
select ename,replace(ename,'S','上海') from emp;
2.2.5 截取EMP表中ename字段的第二个到第三个字符
select ename,substring(ename,2,2) from emp;
2.2.6 以首字母小写的方式显示所有员工的姓名
select ename,concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
三、 数学函数
3.1 简单使用
select abs(520);
select abs(-520);
select bin(520);
select bin(3);
select hex(520)
select hex(16)
select conv(16,10,2);
select conv(16,10,3);
select conv(16,10,4);
select ceiling(3.14);
select ceiling(-3.14);
select floor(3.14);
select floor(-3.14);
select format(3.1415926,2);
select format(-3.1415926,2);
select rand();
select format(rand()*100,0);
select format(rand()*1000,0);
select mod(100,3);
select mod(100,32);
select mod(100,37);
四、其他函数
4.1 简单使用
select user();
select md5('1');
select md5('I LOVE YOU!');
select database();
select ifnull('I Love you,China!','1314520');
select ifnull(null,'1314520');
结尾
如果有什么建议和疑问,或是有什么错误,大家可以在评论区中提出。
希望大家以后也能和我一起进步!!🌹🌹
如果这篇文章对你有用的话,希望大家给一个三连支持一下!!🌹🌹