字段不能在DISTINCT之前,只能在DISTINCT后面
DISTINCT之后有多个字段,按照所有字段进行去重
9.聚合函数
- count(字段):求多少行数据
- sum(字段):求和
- avg(字段):平均数
- max(字段):最大值
- min(字段):最小值
注意:
- varchar能比较大小,不能获取avg(没有任何意义)
- 如果值为Null不参与计算
- sum和avg字段的数据不是数值,结果都是0
-- count(字段):求多少行数据 select count(*) from student; select count(name) from student; -- sum(字段):求和 select sum(score) from student; select sum(job) FROM student; select name+score as sum FROM student; #score的值 SELECT name*score as cheng FROM student; #0 -- avg(字段):平均数 SELECT avg(score) FROM student; -- max(字段):最大值 SELECT max(score) FROM student; SELECT max(job) FROM student; #c -- min(字段):最小值 SELECT min(score) FROM student;
10.拼接
格式1
concat(str1,str2...)
格式2:
concat_WS(separator,str1,str2,...)
-- 格式一:concat(str1,str2...) select CONCAT(id,'-',name) as pj FROM student; -- 格式二:concat_WS(str1,str2...) SELECT CONCAT_WS('~',id,name,score,job)FROM student; #中间以~隔开
11.日期函数
获取当前日期:
current_timestamp;--所有 current_timestamp();--所有 CURRENT_DATE();-- 年月日 CURRENT_DATE;-- 年月日 CURRENT_TIME();-- 时分秒 CURRENT_TIME;-- 时分秒 -- 获取当前日期: -- current_timestamp;--所有 SELECT CURRENT_TIMESTAMP from student; -- current_timestamp();--所有 SELECT CURRENT_TIMESTAMP() from student; -- CURRENT_DATE();-- 年月日 select CURRENT_DATE() from student; -- CURRENT_DATE;-- 年月日 select CURRENT_DATE from student; -- CURRENT_TIME();-- 时分秒 SELECT CURRENT_TIME() FROM student; -- CURRENT_TIME;-- 时分秒 SELECT CURRENT_TIME FROM student;
时间转str
格式:
date_format(date,format)
date:时间
format:格式
str转日期
格式:
str_to_date(str,formaat)
SELECT * FROM date; -- 时间转str -- 格式: -- date_format(date,format) -- date:时间 -- format:格式 select DATE_FORMAT('2021-09-01','%Y~%m~%d'); -- str转日期 -- 格式: -- str_to_date(str,formaat) SELECT STR_TO_DATE('2021-09-01','%Y-%m-%d');
日期相减
格式:
datediff(expr1,expr2);
注意:只能相减年月日,时分秒参与运算结果为null
datediff(expr1,expr2); -- 注意:只能相减年月日,时分秒参与运算结果为null SELECT DATEDIFF('2021-09-09','2021-09-01');
函数向日期添加指定的时间间隔
格式:
DATE_ADD(date,INTERVAL expr unit);
date:时间
INTERVAL:关键字
expr:间隔的数值
unit:年月日时分秒(..,...,day,..,..,..)
SELECT DATE_ADD('2021-09-09',INTERVAL +10 YEAR); SELECT DATE_ADD('2021-09-09',INTERVAL +10 DAY);
12. 数组计算
round(x,d):四舍五入
x:值
d:保留几位小数点
ceil(x):向上取整
floor(x):向下取整
rand():随机数(0-1之间)
-- 数组计算 -- round(x,d):四舍五入 -- x:值 -- d:保留几位小数点 SELECT ROUND(1.3,2); #2表示保留几位小数 -- ceil(x):向上取整 SELECT ceil(1.2); -- floor(x):向下取整 SELECT floor(1.2); -- rand():随机数(0-1之间) SELECT rand();
13.排序
格式:
order by 字段1 asc|desc,字段2 asc|desc...字段n asc|desc;
SELECT * from student ORDER BY score,job; SELECT * from student ORDER BY score desc, job desc;
注意:
- 默认升序asc,降序desc
- 如果有多个字段,按照先后顺序依次排序
14. group by 分组
格式:
group by 字段1,字段2...字段n;
注意:
- 多个字段,按照所有字段进行分组(一起分组)
- 有多少组显示多少条数据(默认情况下,没有经过条件筛选)
- 组显示的数据为每组中默认第一条数据
- by 通常和聚合函数一起使用
select max(score) as c from student where score=c; select max(score) as c from student having score=c; 两个都不能运行 SELECT count(*),job,`name`,id as c from student GROUP BY sex where c>2; #错误 SELECT count(*) as c,job,`name`,id from student GROUP BY sex HAVING c>2; -- select id,name,sex from student where job='a'; # 可以运行 --select id,name,sex from student having job='a'; #不能运行(显示了之后就没有job) -- 执行过程是 from-where-select-having -- select count(*) c from student where c>1; -- 不行 -- select count(*) c from student having c>1;-- 行 select count(*) c,sex from student group by sex where sex='男'; select count(*) c,sex from student group by sex having sex='男'; --where having 一起使用 SELECT count(*)as c,name,id FROM student where sex='男' HAVING c>3; where 是对表中from到的数据进行筛选; having是对表中selec显示数据进行晒选;
到此这篇关于MySQL 基础常用命令总结的文章就介绍到这了,更多相关MySQL常用命令内容请搜索
更多SQL内容来自木庄网络博客