马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
聚合函数
函数阐明COUNT([DISTINCT] expr)返回查询到的数据的 数量SUM([DISTINCT] expr)返回查询到的数据的 总和,不是数字没故意义AVG([DISTINCT] expr)返回查询到的数据的 均匀值,不是数字没故意义MAX([DISTINCT] expr)返回查询到的数据的 最大值,不是数字没故意义MIN([DISTINCT] expr)返回查询到的数据的 最小值,不是数字没故意义
- -- 使用 * 做统计,不受 NULL 影响
- count(*) from students;
- +----------+
- | count(*) |
- +----------+
- | 4 |
- +----------+
- 1 row in set (0.00 sec)
- -- 使用表达式做统计
- select count(1) from students;
- +----------+
- | count(1) |
- +----------+
- | 4 |
- +----------+
- 1 row in set (0.00 sec)
- 统计班级收集的 qq 号有多少
- -- NULL 不会计入结果
- select count(qq) from students;
- +-----------+
- | count(qq) |
- +-----------+
- | 1 |
- +-----------+
- 1 row in set (0.00 sec)
- 统计本次考试的数学成绩分数个数
- --COUNT(math) 统计的是全部成绩
- select count(math) from exam_result;
- +-------------+
- | count(math) |
- +-------------+
- | 7 |
- +-------------+
- 1 row in set (0.00 sec)
复制代码- select sum(math) from exam_result;
- +-----------+
- | sum(math) |
- +-----------+
- | 583 |
- +-----------+
- 1 row in set (0.00 sec)
- -- 不及格 < 60 的总分,没有结果,返回 NULL SELECT SUM(math) FROM exam_result WHERE math< 60;
- select sum(math) from exam_result where math < 60;
- +-----------+
- | sum(math) |
- +-----------+
- | NULL |
- +-----------+
- 1 row in set (0.00 sec)
复制代码- select avg(chinese + math + english) 平均分 from exam_result;
- +--------------------+
- | 平均分 |
- +--------------------+
- | 221.42857142857142 |
- +--------------------+
- 1 row in set (0.00 sec)
复制代码- select MAX(english) from exam_result;
- +--------------+
- | MAX(english) |
- +--------------+
- | 90 |
- +--------------+
- 1 row in set (0.00 sec)
复制代码 返回 > 70 分以上的数学最低分
- select min(math) from exam_result where math > 70;
- +-----------+
- | min(math) |
- +-----------+
- | 73 |
- +-----------+
- 1 row in set (0.00 sec)
复制代码 group by子句的使用
在select中使用group by 子句可以对指定列举行分组查询
语法:select column1, column2, .. from table group by column;
案例:
- 准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)
- 怎样表现每个部分的均匀工资和最高工资
- select deptno,avg(sal),max(sal) from EMP group by deptno;
复制代码
- select avg(sal),min(sal),job, deptno from EMP group by deptno, job;
复制代码
- select avg(sal) from EMP group by deptno
复制代码
- having和group by共同使用,对group by效果举行过滤
- select avg(sal) as myavg from EMP group by deptno having myavg<2000;
- --having经常和group by搭配使用,作用是对分组进行筛选,作用有些像where。
复制代码 实战OJ
牛客:批量插入数据
牛客:找出全部员工当前薪水salary情况
牛客:查找最晚入职员工的全部信息
牛客:查找入职员工时间排名倒数第三的员工全部信息
查找薪水记载凌驾15条的员工号emp_no以及其对应的记载次数t
日期函数
- select current_date();
- +----------------+
- | current_date() |
- +----------------+
- | 2025-03-21 |
- +----------------+
- 1 row in set (0.00 sec)
复制代码
- select current_time();
- +----------------+
- | current_time() |
- +----------------+
- | 09:21:46 |
- +----------------+
- 1 row in set (0.00 sec)
复制代码
- select current_timestamp();
- +---------------------+
- | current_timestamp() |
- +---------------------+
- | 2025-03-21 09:22:22 |
- +---------------------+
- 1 row in set (0.00 sec)
复制代码
- select date_add('2017-10-28',interval 10 day);
- +----------------------------------------+
- | date_add('2017-10-28',interval 10 day) |
- +----------------------------------------+
- | 2017-11-07 |
- +----------------------------------------+
- 1 row in set (0.04 sec)
复制代码
- select date_sub('2017-10-28',interval 10 day);
- +----------------------------------------+
- | date_sub('2017-10-28',interval 10 day) |
- +----------------------------------------+
- | 2017-10-18 |
- +----------------------------------------+
- 1 row in set (0.00 sec)
复制代码
- mysql> select datediff('2017-10-10', '2016-9-1');
- +------------------------------------+
- | datediff('2017-10-10', '2016-9-1') |
- +------------------------------------+
- | 404 |
- +------------------------------------+
- 1 row in set (0.00 sec)
复制代码 案例-1:
- create table tmp(
- id int primary key auto_increment,
- birthday date
- );
复制代码
- mysql> insert into tmp(birthday) values(current_date());
- Query OK, 1 row affected (0.01 sec)
- mysql> select * from tmp;
- +----+------------+
- | id | birthday |
- +----+------------+
- | 1 | 2025-03-21 |
- +----+------------+
- 1 row in set (0.00 sec)
复制代码
案例-2:
- mysql> create table msg (
- id int primary key auto_increment,
- content varchar(30) not null,
- sendtime datetime
- );
复制代码 插入数据
- mysql> insert into msg(content,sendtime) values('hello1', now());
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into msg(content,sendtime) values('hello2', now());
- Query OK, 1 row affected (0.00 sec)
复制代码
- 表现全部留言信息,发布日期只表现日期,不消表现时间
- select content,date(sendtime) from msg;
复制代码
- select * from msg where date_add(sendtime, interval 2 minute) > now();
- 理解:
- ------------------------------|-----------|-------------|------------------
- 初始时间 now() 初始时间+2min
复制代码 字符串函数
案例:
- select charset(ename) from EMP;
复制代码
- 要求表现exam_result表中的信息,表现格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
- select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from student;
复制代码
- select length(name), name from student;
复制代码 注意:length函数返回字符串长度,以字节为单元。假如是多字节字符则盘算多个字节数;假如是单字节字符则算作一个字节。好比:字母,数字算作一个字节,中文表现多个字节数(与字符集编码有关)
- select replace(ename, 'S', '上海') ,ename from EMP;
复制代码
- select substring(ename, 2, 2), ename from EMP;
复制代码
- select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from EMP;
复制代码 数学函数
- select format(12.3456, 2);
复制代码
获取0 ~ 99的随机数
别的函数
- md5(str)对一个字符串举行md5择要,择要后得到一个32位字符串
- select md5('admin')
- +----------------------------------+
- | md5('admin') |
- +----------------------------------+
- | 21232f297a57a5a743894a0e4a801fc3 |
- +----------------------------------+
复制代码
- password()函数,MySQL数据库使用该函数对用户加密
- select password('root');
- +-------------------------------------------+
- | password('root') |
- +-------------------------------------------+
- | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
- +-------------------------------------------+
复制代码
- ifnull(val1, val2) 假如val1为null,返回val2,否则返回val1的值
- select ifnull('abc', '123');
- +----------------------+
- | ifnull('abc', '123') |
- +----------------------+
- | abc |
- +----------------------+
- 1 row in set (0.01 sec)
-
- select ifnull(null, '123');
- +---------------------+
- | ifnull(null, '123') |
- +---------------------+
- | 123 |
- +---------------------+
- 1 row in set (0.00 sec)
复制代码 |