mysql查询--高级查询

100 阅读1分钟

distinct 不重复前提

从teacher表中查找不重复的Depart 

select distinct Depart from teacher

between  ...and...  在。。。之间

从Score表中查找degree在60-80之间的所有信息 

select *  from Score where Degree between 60 and 80

in+元组  在...在 其中一个 

从score表中查找degree是85或86或88的所有信息

select *  from Score where Degree in(85,86,88)

avg(column) 平均值

 查询每门课的平均成绩

select Cno,avg(degree) from Score group by Cno

null  为空 

因为null类型比较特殊,必须使用 is 来比较 

这里的is 不能用“ = ”替换

从emp表中查询comm为空的empno,ename,comm

select empno,ename,comm from emp where comm is null; 

not   取反

is not null             不能是       not is null

从emp中查找comm不为空的empno,ename,comm

select empno,ename,comm from emp where comm is not null; 

and > or  优先级

从emp表中查询sql大于1800,并且 deptno =20 或等于30 的全部信息

select * from emp where sal>1800 and (deptno=20 or deptno=30); 

in  在...中  表示包含  比or 简洁

从表emp中查询job是SALESMAN 或者 MANAGER 的人的ename,job

select ename,job from emp where job in (“SALESMAN”,“MANAGER”); 

查询出薪水不是1600和3000的员工  

select ename,sal from emp where sal not in (1600,3000); 

like _ 表示一个字符

查询姓名中第二个字符为A的所有员工

select ename from emp where ename like “_A%”;