函数 | 说明 |
---|---|
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)
在select中使用group by 子句可以对指定列进行分组查询
语法:select column1, column2, .. from table group by column;
案例:
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
select avg(sal) as myavg from EMP group by deptno having myavg<2000;
--having经常和group by搭配使用,作用是对分组进行筛选,作用有些像where。
牛客:批量插入数据 牛客:找出所有员工当前薪水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;
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 abs(-100.2);
select ceiling(23.04);
select floor(23.7);
select format(12.3456, 2);
select rand();
获取0 ~ 99的随机数
select user();
select md5('admin')
+----------------------------------+
| md5('admin') |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
select database();
select password('root');
+-------------------------------------------+
| password('root') |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
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)