字段进行算术运算

格式:
	(字段 符号 字段)
例如:
	select (name+age) from students;
注意:
	字符串参与运算字符串为0参与运算

拼接:

格式:
	concat(str1,str2...)
例如:把name和age以-拼接显示
	select concat(name,'-',age)from students;
格式:
	concat_WS(separator,str1,str2,...)
例如:把name和age以-拼接显示
	select concat('-',name,age)from students;

日期函数

获取当前日期:
	current_timestamp;--所有
	current_timestamp();--所有
	CURRENT_DATE();-- 年月日
	CURRENT_DATE;-- 年月日
	CURRENT_TIME();-- 时分秒
	CURRENT_TIME;-- 时分秒

时间转str
	格式:
		date_format(date,format)
		date:时间
		format:格式
str转日期
	格式:
		str_to_date(str,formaat)

日期相减

格式:
	datediff(expr1,expr2);
注意:只能相减年月日,时分秒参与运算结果为null

函数向日期添加指定的时间间隔

格式:
	DATE_ADD(date,INTERVAL expr unit);
	date:时间
	INTERVAL:关键字
	expr:间隔的数值
	unit:年月日时分秒(..,...,day,..,..,..)

数组计算

round(x,d):四舍五入
	x:值
	d:保留几位小数点
ceil(x):向上取整
floor(x):向下取整
rand():随机数(0-1之间)

排序

格式:
	order by 字段1 asc|desc,字段2 asc|desc...字段n asc|desc;
例如:按照age进行降序排列,age相同按照id进行降序排列
	select * from students order by age desc,id desc;
注意:
	默认升序asc,降序desc
	如果有多个字段,按照先后顺序依次排序

group by 分组

格式:
	group by 字段1,字段2...字段n;
注意:
	多个字段,按照所有字段进行分组(一起分组)
	有多少组显示多少条数据(默认情况下,没有经过条件筛选)
	每组显示的数据为每组中默认第一条数据
	gruop by 通常和聚合函数一起使用

筛选:where having

区别:having可以使用聚合函数
例如:
	select * from students where age>=25;可以
	select * from students having age>=25;可以
	select sex,count(*) c from students group by sex  where c>4;不可以
	select sex,count(*) c from students group by sex having c>4;可以

TopN:前几条数据

1.TopN	age最大的前三个
	select * from students order by age desc limit 0,3;
2.分组Top1 按sex分组后,求分组中年龄最大的一个
	1.select * from students where age in (select max(age) m from students group by sex);
	2.select * from students as stu1 where age=(select max(age) from students as stu2 where stu1.sex=stu2.sex);

2.分组TopN 按sex分组后,求分组中年龄最大的三个
	select * from students as stu1 where 3>(select count(*) students as stu2 where stu1.sex=stu2.sex and stu1.age<stu2.age);

mysql三大范式

1.原子性:字段不可在分割
2.唯一性:字段依赖于主键
3.冗余性:数据量过大
posted on 2021-09-13 19:21  学海无涯,书山有路  阅读(30)  评论(0)    收藏  举报