MySQL数据库 单表查询 索引的分类创建和删除 数据的插入更新和删除 创建emp表并插入数据 单表查询 包含条件查询 分页查询 查询结果排序 函数的使用包含单行函数 多行函数

1,索引的概念

是数据库对象,实现数据库的快速查询

使用索引的原因:实现数据库的快速查询,提高查询速度

2,索引的分类


1,普通索引
		最基本的索引,对字段数据的类型和值没有任何限制,数据类型可以任意,字段的值可以为空也可以重复。

2,主键索引
		给主键字段添加的索引、
		主键特点:唯一且非空

3,唯一索引
		给唯一字段添加的索引
				唯一索引和主键索引的区别:
				唯一索引:只有唯一,可以有空值
				主键索引:唯一且非空
				
4,全文索引
		适用于给一大串文本添加的索引,只可以给字符串数据类型添加
		字符串数据类型(char varchar text)
		
5,空间索引
		字段的数据类型只能是空间数据类型,且改字段的值必须为 非空 not null
		空间数据类型 geometry point linestring polygon
		
6,复合索引
		给多个字段添加的索引
		注意:如果使用了复合索引,查询条件中只有使用了第一个字段,该索引才会被触发
		例如(id name)只有查询条件中使用了id字段,索引才会被使用
		如果查询条件只有name字段,则索引不会被触发

3,创建索引

1,自动创建索引
	在创建表的时候,给表添加了主键和唯一约束时,数据库给自动的给主键约束和唯一约束创建对应的主键索引和唯一索引
			create table index_student(
			sno int(8) primary key auto_increment,
			sname varchar(4) unique,
			age int(2)
			);
	查询表中的索引语法为:show index from 表名
	show index from index_student;
	
2,手动创建索引

		a,创建表时创建索引
		
		
				1,创建普通索引、
						create table 表名(
						字段名1 字段类型1,
						....,
						index|key [索引名] [索引类型] (字段名[(长度)] [asc|desc])
						);
				create table index_student(
				sno int(8),
				sname varchar(20),
				age int(2),
				index (sno)
				);
				show index from index_student;

		
				2,唯一索引的创建
						create table 表名(
						字段名1 字段类型1,
						....,
						unique index|key [索引名] [索引类型] (字段名[(长度)] [asc|desc])
						);
									create table index_student(
									sno int(8),
									sname varchar(20),
									age int(2),
									unique index (sno)
									);
									show index from index_student;


				3,创建主键索引
						create table表名(
						字段名1 字段类型1,
						....,
						primary index|key [索引名] [索引类型] (字段名[(长度)] [asc|desc])
						);				
									create table index_student(
									sno int(8),
									sname varchar(20),
									age int(2),
									primary key (sno)
									);
									show index from index_student;



				4,创建全文索引、只有:只能给字符串数据类型添加
						create table 表名(
						字段名1 字段类型1,
						....,
						fulltext index|key [索引名] [索引类型] (字段名[(长度)] [asc|desc])
						);		
								
										create table index_student(
										sno int(8),
										sname varchar(20),
										age int(2),
										sinfo varchar(200),
										fulltext key (sinfo)
										);
										show index from index_student;		



				5,创建复合索引
						create table 表名(
						字段名1 字段类型1,
						....,
						index|key [索引名] [索引类型] (字段名1[(长度)] [asc|desc], 字段名2[(长度)] [asc|desc], 字段名3[(长度)] [asc|desc])
						);		
						
								---创建表index_student,给sno 和 sname 添加复合索引
								create table index_student(
								sno int(8),
								sname varchar(20),
								age int(2),
								sinfo varchar(200),
								index (sno,sname)
								);

								show index from index_student;						


				6,创建空间索引
				注意:只能给空间数据类型添加,且该字段的值不能为空 not null
						create table 表名(
						字段名1 字段类型1,
						....,
						spatial index|key [索引名] [索引类型] (字段名[(长度)] [asc|desc])
						);	
						
							---创建表index_student,给sloc字段(是point字段类型)添加空间索引
							create table index_student(
							sno int(8),
							sname varchar(20),
							age int(2),
							sinfo varchar(200),
							sloc point not null,
							spatial index (sloc)
							);
							
							show index from index_student;

				
		b,创建表后使用“create index ”创建索引
		create [ unique|fulltext|spatial ] index 索引名称 [索引的类型] on 表名 (字段名1[(长度)] [asc|desc], 字段名2[(长度)] [asc|desc])

		需要注意的是:使用create index这种创建索引的方式不能创建主键索引

		(1),创建普通索引
						---创建表index_student,给表sno添加普通索引
						create table index_student(
						sname varchar(8),
						sno int(5),
						age int(2)
						);
						create index index_student_sno on index_student (sno);
						show index from index_student;


		(2),创建唯一索引
						---创建表index_student,给表sname添加唯一索引
						create table index_student(
						sname varchar(8),
						sno int(5),
						age int(2)
						);
						create unique index index_student_sname on index_student (sname);
						show index from index_student;


		(3),创建全文索引 fulltext
					---创建表index_student,给表sinfo添加全文索引
					create table index_student(
					sname varchar(8),
					sno int(5),
					age int(2),
					sinfo varchar(200)
					);
					create fulltext index index_student_sinfo on index_student (sinfo);
					show index from index_student;
		

		(4),创建空间索引
					---创建表index_student,给表sloc添加空间索引
					create table index_student(
					sname varchar(8),
					sno int(5),
					age int(2),
					sloc point not null
					);
					create spatial index index_student_sloc on index_student (sloc);
					show index from index_student;


		(5),创建复合索引
					---创建表index_student,给表sno和sname添加复合索引
					create table index_student(
					sname varchar(8),
					sno int(5),
					age int(2)
					);
					create index index_student_sno_sname on index_student (sno, sname);
					show index from index_student;	


c,给已有表添加索引“alter table”
		
		(1)添加普通索引
		语法为:alter table 表名 add index|key [索引名] [索引类型] (字段名 [长度] [asc | desc])
				---创建表index_student,给表sno添加普通索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2)
				);
				alter table index_student add index (sno)
				show index from index_student;


		(2)添加唯一索引
		语法为:alter table 表名 add unique [index|key] [索引名] [索引类型] (字段名 [长度] [asc | desc])
				---创建表index_student,给表sname添加唯一索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2)
				);
				alter table index_student add unique index (sname);
				show index from index_student;


		(3)添加主键索引
		语法为:alter table 表名 add primary key [index|key] [索引名] [索引类型] (字段名 [长度] [asc | desc])
				---创建表index_student,给表sno添加主键索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2)
				);
				alter table index_student add primary key (sno);
				show index from index_student;
		
		
		(4)添加全文索引
		语法为:alter table 表名 add fulltext [index|key] [索引名] [索引类型] (字段名 [长度] [asc | desc])
				---创建表index_student,给表sinfo添加全文索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2),
				sinfo varchar(200)
				);
				alter table index_student add fulltext index(sinfo);
				show index from index_student;		


		(5)添加空间索引
		语法为:alter table 表名 add spatial [index|key] [索引名] [索引类型] (字段名 [长度] [asc | desc])
				---创建表index_student,给表sloc添加空间索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2),
				sloc point not null
				);
				alter table index_student add spatial index (sloc);
				show index from index_student;		


		(6)添加复合索引
		语法为:alter table 表名 add index|key [索引名] [索引类型] (字段名1 [长度] [asc | desc], 字段名2 [长度] [asc | desc])
				---创建表index_student,给表sno和sname添加复合索引
				create table index_student(
				sname varchar(8),
				sno int(5),
				age int(2)
				);
				alter table index_student add index (sno,sname);
				show index from index_student;	

4,删除索引

1,使用alter table 删除
		语法为alter table 表名 drop index|key 索引名称
		alter table index_student drop index sno;
		
2,使用drop index 删除
		语法为drop index 索引名称 on 表名
		
注意:使用alter table 方式不能删除主键索引
  删除主键索引的方式为:
  a,	alter table 表名 drop primary key
  b,	使用drop index进行删除

5,数据的插入

(1)为所有字段插入数据
		语法为:insert [into] 表名 [(字段名1,字段名2,字段名3,....)] values|value (值1,值2,值3....)
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				---为表中所有字段插入数据
				insert into student (sno,sname,age,sex,email) values (1,"张三", 20, "男", "zhangsan@163.com");
				
				---另一种写法表后面不写字段名,则插入值的顺序要与表结构相同
				insert into student values (2,"李四", 21, "女", "lisi@163.com");
				---查询表student中的数据
				select * from student;



(2)为指定字段插入数据
		语法为:insert [into] 表名 (字段名1,字段名2,字段名3,....) values|value (值1,值2,值3....)
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				---给表中sname和age插入数据
				insert into student (sname,age) values ("王五", 18);
				
				---查询表student中的数据
				select * from student;


(3)使用SET方式插入数据
		语法为:insert into 表名 set 字段名1=字段值1,字段名2=字段值2,....;
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				---使用set方式给表中所有字段插入数据
				insert into student set sno=4,sname="皮皮",age=17,sex="男",email="pipi@16.com";
				
				---使用set方式给指定的字段插入数据
				insert into student set sname="彬彬",age=2,email="binbin@163.com";
				
				---查询表student中的数据
				select * from student;
				

(4)同时插入多条数据
		语法为:insert [into] 表名 [(字段名1,字段名2,字段名3,....)] values|value (值1,值2,值3....),(值1,值2,值3....),....;
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				
				---给所有字段插入多条数据
				insert into student (sno,sname,age,sex,email) 
				values (6,"小红",15,"女","xiaohong@163.com"),(7,"小二",17,"男","xiaoer@163.com");
				
				---给指定字段插入多条数据
				insert into student (sname,age,email) 
				values ("小花",14,"xiaohua@163.com"),("小强",11,"xiaoqiang@163.com");
				
				---查询表student中的数据
				select * from student;



(5)插入查询结果
		语法为:insert [into] 表名 (字段名1,字段名2,字段名3,....) select 字段名1,字段名2,字段名3.....from 表 where 条件;
		注意:插入表的字段和查询的字段个数和类型要保持一致
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				
				---创建t_student表
				create table t_student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "女",
				email varchar(20) unique
				);
				
				---将student表中的数据查询结果插入到t_student表中
				insert into t_student select * from student;
				
				select * from t_student;

6,数据的更新

1,更新指定数据
		语法为:update 表名 set 字段名1=值1[,字段名2=值2,....] where 条件
		
				---创建一张表student(sno主键 自动增长 sname 非空 age sex默认男 email唯一)
				create table student(
				sno int(8) primary key auto_increment,
				sname varchar(20) not null,
				age int(8),
				sex varchar(1) default "男",
				email varchar(20) unique
				);
				
				---更新指定数据
				update student set sname="张老大" where sname="张三";
				select * from student;
				

2,更新全部数据
		语法为:update 表名 set 字段名1=值1[,字段名2=值2,....] 

				---更新全部数据 将表中年龄都修改为18
				update student set age=18;
				select * from student;

7,数据的删除

1,删除指定数据
		语法为:delete from 表名 where 条件
		
			---删除student表中sno大于2的学生信息
			delete from student where sno>2;


2,删除全部数据
		语法为:delete from 表名 where 条件
		
				---删除student表的全部数据
				delete from student;


3,truncate关键字删除数据
		语法为:truncate 表名

4,truncate与delete删除全部记录的区别:

		a,delete是数据操纵语言DML 而truncate是数据定义语言DDL
		
		b,delete删除数据是一条一条删除
			truncate删除数据是保留表结构,直接删除数据,删除后表的状态相当于新表
			truncate的效率比delete高
			
		c,delete删除数据,会显示删除行数
			truncate不会显示
			
		d,删除记录后,再次向表添加数据,自增的值会在原来最大值+1
			truncate删除记录后,会从1开始自增
			
		e,delete操作可以回滚即删除的数据可以恢复
			truncate操作隐士提交,删除的数据不能恢复

8,创建emp表并插入数据以及表的查询

1,单表查询



		用户用不同的方式从数据库中获取自己所需要的数据
		是数据库操作中非常重要且频繁的操作

---创建新库test
---在test库中创建emp(雇员表)
emp(雇员编号empno
		雇员名称ename
		雇员职位job
		雇员领导mgr
		雇员入职日期hiredate
		雇员月薪sal
		雇员津贴comm
		雇员部门编号deptno
		
create database test;
create table emp(
empno int(4) primary key,
ename varchar(10),
job varchar(9),
mgr int(4),
hiredate date,
sal decimal(7,2),
comm decimal(7,2),
deptno int(2)
);

---同时插入多条数据

insert into emp values
(7369, 'Smith', 'clerk', 7902, '1980-12-17', 800, null, 20), (7499, 'Allen', 'salesman', 7698, '1981-02-20', 1600, 300, 30), (7521, 'Ward', 'salesman', 7698, '1981-02-22', 1250, 500, 30), (7566, 'Jones', 'manager', 7839, '1981-04-02', 2975, null, 20), (7654, 'Maritn', 'salesman', 7698, '1981-09-28', 1250, 1400, 30), (7698, 'Blake', 'manager', 7839, '1981-05-01', 2850, null, 30), (7782, 'Clark', 'manager', 7839, '1981-06-09', 2450, null, 10), (7788, 'Scott', 'analyst', 7566, '1987-04-19', 3000, null, 20), (7839, 'King', 'president', null, '1981-11-17', 5000, null, 10), (7844, 'Turner', 'salesman', 7698, '1981-09-08', 1500, 0, 30), (7876, 'Adams', 'clerk', 7788, '1987-05-23', 1100, null, 20), (7900, 'James', 'clerk', 7698, '1981-12-03', 950, null, 30), (7902, 'Ford', 'analyst', 7566, '1981-12-03', 3000, null, 20), (7934, 'Miller', 'clerk', 7782, '1982-01-23', 1300, null, 10);

select * from emp;



2,以上表创建完成之后进行单表查询

	a,查询所有字段
	语法1为:select 字段名1,字段名2,字段名3,....from 表名
	语法2为:select * from 表名

	b,查询指定字段
	语法为:select 字段名1,字段名2,字段名3,....from 表名
			---查询雇员表中雇员姓名,职位,部门号,薪资
			select ename,job,deptno,sal from emp;

	c,去重distinct的使用
	语法为:select distinct 字段名1,字段名2,字段名3,....from 表名	
			---查询雇员在哪个部门号中
			select distinct deptno from emp;
			---查询雇员部门号及职位
			select distinct deptno,job from emp;
		注意:distinct使用到多个字段上时,只有多个字段的值都相同时才去重

	
	d,查询时算术运算符的使用
	 +   -   *  /(div)   v%(mod)
			---查询每个雇员的年薪
			select ename,sal*12 from emp;

	e,给字段起别名
	语法为:select 字段名1 [as] 别名,字段名2,字段名3,....from 表名;
			---查询每个雇员的年薪,别名为yearsal
			select ename,sal*12 as yearsal from emp;
	注意:如果别名中有空格特殊符号,需要使用单引号括起来
			---查询每个雇员的年薪,别名为yearsal&年薪 或者别名为 yearsal 年薪
			select ename,sal*12 as 'yearsal&年薪' from emp;
			select ename,sal*12 as 'yearsal 年薪' from emp;

2,查询结果排序

默认升序 asc
降序 desc

1,在单个字段中排序
	语法为:select 查询内容 from 表名 order by 字段  asc|desc
			---查询雇员信息按雇员薪资升序排序
			select * from emp order by sal asc;
			---由于默认的排序方式是升序,所以asc可以省略
			select * from emp order by sal;
			
2,在多个字段中使用排序
	语法为:select 查询内容 from 表名 order by 字段名1  asc|desc,字段名2  asc|desc....
			---查询雇员信息,先按雇员薪资升序排,如薪资相同则按入职日期降序排序
			select * from emp order by sal,hiredate desc;
	注意:排序可以起别名
			---查询雇员的年薪给年薪起个别名yearsal,按年薪降序排序
			select ename,sal*12 as yearsal from emp order by yearsal desc;

3,条件查询

是用户按照一定条件查询,查询满足条件的部分记录
语法格式:select 查询内容 from 表 where 条件

(1)比较运算符的使用   =    >    <      >=      <=      !=       <>
			---查询部门号为20的雇员信息
			select * from emp where deptno=20;
			---查询薪资大于等于3000的雇员信息
			select * from emp where sal>=3000;
			---查询薪资小于3000的雇员名称,薪资,职位
			select ename,sal,job from emp where sal<3000;
			---查询名为Smith的雇员信息
			select * from emp where ename="Smith";
	注意:MySQL默认不区分大小写,如果要区分大小写则可以使用binary关键字
			---binary的使用
			select * from emp where binary ename="Smith";
			
(2) [not] between and的使用 
		可以查询某区间范围的记录
			---查询薪资在1200-3000之间的雇员信息
			---between后面是开始值,and后面是结束值,不能颠倒。包括边界
			select * from emp where sal between 1200 and 3000;
			---查询薪资不在1200-3000范围之内的雇员姓名,薪资
			select ename,sal from emp where sal not between 1200 and 3000;

(3)[not] in的使用
	语法格式为:select 查询内容 from 表名 where 字段名 in(值1,值2,值3,...)
			---查询雇员姓名是smith,scott,king的雇员信息
			select * from emp where ename in("smith","scott","king");
			---查询雇员姓名不是smith,scott,king的雇员信息
			select * from emp where ename not in("smith","scott","king");

(4)is null 或者 is not null  判断某个字段是否为空
			---查询雇员中,有津贴的雇员信息
			select * from emp where comm is not null;
			---查询雇员中,没有津贴的雇员信息
			select * from emp where comm is null;

(5)模糊查询
		可查询以XX开头,以XX结尾,包含XX,第几个是X
		%:指任意长度的字符
		_:匹配一个字符
		语法为:select 查询内容from表名where字段名like
				---查询雇员姓名以S开头的雇员信息
				select * from emp where ename like"S%";
				---查询雇员姓名以S结尾的雇员信息
				select * from emp where ename like"%S";
				---查询雇员姓名包含S的雇员信息
				select * from emp where ename like"%S%";
				---查询雇员姓名第二个字符是L的雇员信息
				select * from emp where ename like"_L%";
				---查询雇员姓名长度是5的雇员信息
				select * from emp where ename like"_____";

(6)条件中的逻辑运算符
		and:查询时,只有满足所有条件的记录才会被查询出来
		select 查询内容from表名where 条件1 and 条件2 and....
				---查询雇员表中部门号为20 职位是clerk的雇员姓名 部门编号 职位
				select ename,deptno,job from emp where deptno=20 and job="clerk";
				---查询部门号为30,薪资在1500-3000之间的雇员信息
				select * from emp where deptno=30 and sal between 1500 and 3000;
				
		or:查询时,只要满足任一条件记录就能被查询出来
		select 查询内容from表名where 条件1 or 条件2 or....
				---查询雇员表中部门号为20 或者职位是clerk的雇员姓名 部门编号 职位
				select ename,deptno,job from emp where deptno=20 or job="clerk";

4,分页查询

select 查询内容 from 表名 where 条件 order by 字段名 asc|desc limit a,b 
注意:
	(a代表从哪条记录开始,b代表每页显示的条数) 且默认第一条数据的值为0
	如果limit与order by 连用的时候,先排序再分页
		---显示第一页雇员信息 例如每页显示4条
		select * from emp limit 0,4;
		---显示第二页雇员信息 每页显示4条
		select * from emp limit 4,4;
		---显示第三页雇员信息 每页显示4条
		select * from emp limit 8,4;
		---显示第n页雇员信息 每页显示4条
		select * from emp limit (n-1)*4,4;

9,函数的使用

(1)单行函数

a,字符函数 
		(1)concat() 拼接字符串
			---查询雇员信息,以指定格式输出
			雇员姓名:XX,薪资:XX,职位:XX,入职日期:XX,年薪:XX,
			select concat("雇员姓名:",ename,",薪资:",sal,",职位:",job,",入职日期:",hiredate,",年薪:",sal*12) from emp;
		
		(2)length()计算字符串的长度
			---查询雇员姓名长度是5的雇员信息
			select * from emp where length(ename)=5;
			
		(3)lower()  upper()转换大小写
			---查询雇员姓名 大写的雇员姓名 小写的雇员姓名
			select ename,lower(ename),upper(ename) from emp;		
			
		(4)replace()在指定的字符串中,将某子串替换为新的字符串
			replace(目标字符串,查找的子串,新字符串)
				---将helloWord字符串中的hello替换为hi
				select replace("helloworld","hello","hi") from dual;
				---另一种写法为
				select replace("helloworld","hello","hi");
				
		(5)substring()截取子串
			substring(目标字符串,开始位置,长度)  注意开始索引是从1开始的。
				---截取雇员姓名的前4个字符
				select substring(ename,1,4) from emp;


b,数值函数
	1,abs()  作用是:取绝对值
			select(1),(-1);

	2,pi()  作用是:获取圆周率
			select pi();

	3,mod() 作用是:取余
			select mod(3,2);
	
	5,pow() 作用是:求一个数的n次方
			select pow(3,2)

	6,ceil() 向上取整  floor()向下取整
			select ceil(5.43),floor(5.7);

	7,round(num) 代表返回四舍五入的整数
	round(num,n)返回四舍五入n位小数
		select round(5.4),round(6.8),round(5.34,1),round(6.789,2);
		
	8,truncate(num,n) n的值可以为0,1,2,3,4...其中n为0时代表截取整数
		select truncate(5.3,0),truncate(5.67,1),truncate(5.789,2);
	
	9,rand() 获取浮点类型的随机数,范围是0-1.0。包括0但不包含1
			select rand(),rand(),rand();


c,时间日期函数
	1,now() 获取SQL执行时当前日期和时间 包括年月日 时分秒
			select now();

	2,curdate()获取当前日期 只包括年月日
			select curdate()
			
	3,curtime()获取当前时间 只包括时分秒
			select curtime();
	
	4,sysdate()获取函数执行时的日期和时间
		select sysdate(),now(),sleep(3),sysdate(),now();
	
	5,dayofyear()获取某个日期是所在年份的第几天
	 week()获取某个日期是所在年份的第几周
	 	select dayofyear(now()),week(now());
	
	6,datediff()计算两个日期之间的时间间隔
		---计算2019.1.1日距离现在时间间隔
		select datediff("2019-12-30",now());

	7,date_add(),date_sub() 实现日期的加减运算
		date_add(日期, 时间间隔类型关键字interval, 时间间隔类型对应的表达式, 时间间隔类型)
		 day_hour  1_12 代表1天12个小时
		 year_month 2_1 代表2年1个月
		 
		 select date_add(now(), interval "2_1" year_month)
		 select date_sub(now(), interval 10 day);

e,流程控制函数
	1,if(条件,t,f) 如果条件成立返回t,否则返回f
		select if(1>2,"1大于2","1小于2");
			---查询雇员的薪资,如果薪资>=3000,输入“高薪”,否则“低薪”
			select sal,if(sal>=3000,"高薪","低薪") "薪资水平" from emp;
			
	2,ifnull(值1,值2) 如果值1不为空则返回值1,否则返回值2
			---查询雇员的年薪
			select sal,comm,(sal+comm)*12 from emp; 此语句达不到想要的效果
			select (sal+ifnull(comm,0))*12 from emp;
			select sal,comm,(sal+ifnull(comm,0))*12 from emp;
	3,nullif(值1,值2) 如果值1等于值2返回null,否则返回值1
			select nullif(1,2),nullif(1,1);
	
	4,	   case 值
			when 值1 then 结果1
			when 值2 then 结果2
			...
			else 其他结果
			end
 		select case 1 when 1 then "这是结果1" when 2 then "这是结果2" else "这是其他结果" end;

	5,   case 
			when 条件 then 结果1
			when 条件 then 结果2
			...
			else 其他结果
			end
示例为:---查询雇员薪资,如果薪资>=3000返回“高薪”,否则“低薪”
		select sal,case
		when sal>=3000 then "高薪"
		else "低薪"
		end "薪资水平"
		from emp;

(2)多行函数

定义:操作一组数据(多行记录)返回一个结果,也叫分组函数
大多用于统计
例如:统计各部门中雇员的人数。统计各部门中最高和最低薪资

1,count()统计表中记录的数目
		---查询emp表中有多少条记录
		select count(*) from emp;

	另一条语法是count(exp)统计exp值非空的记录数目
	---查询雇员表中,有多少位雇员有津贴
	select count(comm) from emp;

	另一条语法是count(distinct(exp)) 返回表达式exp的值不重复且非空的总记录数目
		---统计雇员表中有多少位雇员是领导
		select count(distinct(mgr)) from emp;--统计的是除董事长外的领导人数
		---统计雇员表中包含董事长的领导数 需要用到ifnull函数
		select count(distinct(ifnull(mgr,1))) from emp;

2,sum(exp) 返回表达式值的总和
	select sum(sal) from emp;
	
	另一条语法是sum(distinct(exp))返回不重复的表达式exp的总和
	select sum(sal),sum(distinct(sal)) from emp;

3,avg(exp)返回表达式值的平均值
	select avg(sal) from emp;

	另一条语法是avg(distinct(exp))返回不重复的表达式exp的平均值
	select avg(distinct(sal)) from emp;

4,max()  min() 
	max(emp)返回表达式值的最大值
	min(emp)返回表达式值的最小值
	select min(sal),max(sal) from emp;

(3)分组统计

语法为:
		select 查询内容
		from 表名
		[where 条件]
		[group by 分组字段名1,分组字段名2,...]
		[order by 字段名 asc|desc]
		[limit]

示例如下:
	 ---求每个部门的人数
	 select deptno,count(*) from emp  group by deptno;
	 ---求每个部门的平均工资
	 select deptno,avg(sal) from emp group by deptno;
	 ---求每个部门中最高工资和人数
	 select deptno,max(sal),count(*) from emp group by deptno;
	 ---求每个岗位的人数
	 select job,count(*) from emp group by job;
	 ---显示每个部门不同岗位的人数
	 select deptno,job,count(*) from emp group by deptno,job;

注意事项1:如果查询字段,没有在多行函数中,则必须是分组字段
	select ename,job,sum(sal) from emp group by job; 此语句运行后会报错

注意事项2:如果没有group by 则查询字段不能与多行函数一起查询
	select sal,empto from emp; 合法
	select sum(sal),empto from emp;不合法

注意事项3:不允许在where条件中使用多行函数

having子句

语法为:
		select 查询内容
		from 表名
		[where 条件]
		[group by 分组字段]
		[having 条件]
		[order by]
select from ---where过滤---group by---having过滤
示例如下:
	---每个部门不同岗位的人数,且人数大于2
	select count(*),deptno,job from emp group by deptno,job having count(*)>2;
	---在emp表中列出工资最小值小于2000的职位
	select job,min(sal) from emp group by job having min(sal)<2000;
	---查询每个职位的最低薪资
	select job,min(sal) from emp group by job;
	---列出平均工资大于1200的部门和职位搭配组合
	select avg(avg) from emp group by deptno,job having avg(sal)>1200;
	---求每个部门不同职位的平均工资
	select avg(sal) from emp group by deptno,job;
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值