select语法:数据库
select [distinct|all] 列名 from 表名 [where] [group by] [having] [order by] ps:[] 表示能够省略网络
举几个栗子:oracle
select * from emp; ps:* 表示全部字段即把要查询的表的全部字段都显示出来,并不建议使用由于网络消耗大,效率也不高函数
select empno from emp; ps:筛选指定字段,能够筛选多个,字段之间用逗号隔开spa
select empno,ename,job from emp; ps:筛选指定字段,能够筛选多个,字段之间用逗号隔开3d
select job from emp;blog
select distinct(job) from emp; ps:all是默认的即有重复也会显示,distinct消除重复排序
select * from emp where comm is not null; ps:null表示空值class
select job,count(*),sum(sal) from emp group by job; ps:count() sum()为聚合函数后面会讲效率
select sum(sal), count(*), (sum(sal)/count(*)) from emp group by job having (sum(sal)/count(*))>2000; ps:having 要与group by 连用 having 要放在group by后面,group by 能够单独使用
select * from emp order by empno desc; ps:desc是按降序排序,asc是按升序排序,默认是asc
select empno as 雇员编号 from emp order by 雇员编号; ps:雇员编号是别名,别名中英文不限,固然你要用阿拉伯语,德语什么的只要能够打出来识别也没问题,as能够省略也能够写,都是同样的道理,order by后应该使用别名,只有order by 能够这样,having后面都不能够
select * from emp order by empno desc ,job; ps:能够根据两个字段进行排序,先按照empno进行降序排序,若是相等对job进行升序排序
select job,sum(sal) from emp group by job ; ps:group by 后的字段必须在查询字段出现,查询字段还能够出现聚合函数
select job,sum(sal)*2 from emp group by job ; ps:不表示数据库的数据被改变只表示显示的结果
select ename ,sal from emp where sal not between 4000 and 5000; ps:between 4000 and 5000 至关于 >=4000 and <=5000
select job , ename from emp where sal in(800,1600,1500); ps:在集合中选取符合条件的
select ename from emp where ename like '__A%'; ps:模糊查询,_表明匹配一个字符,%表明匹配至少0 个字符
select ename from emp where ename like '%A%' or ename like '%E%';
使用where来进行筛选时,经常使用的操做符:< 小于 >大于 = 等于 !=不等于 <>不等于 <=小于等于 >=大于等于
having是对group分的组进行筛选,不一样于where group要分的组是where筛选事后的
子查询:
select empno, ename from emp where empno in (select empno from emp where comm is not null);
any 和<any <=any表示小于或小于等于列表中最大值,与 in配合使用 和> any >=any表示大于或大于等于列表中的最小值 =any 至关于in
all 和<all <=all表示小于或小于等于列表中的最小值,与in配合使用 和>all >=all表示大于或大于等于列表中的最大值 <>all至关于 not in
举几个栗子:
select sal from emp where comm is not null;(1)
select * from emp where sal =any(select sal from emp where comm is not null);(2)
select * from emp where sal in(select sal from emp where comm is not null);(3)
select * from emp where sal <any(select sal from emp where comm is not null);
select * from emp where sal <=any(select sal from emp where comm is not null);
select * from emp where sal >any(select sal from emp where comm is not null);
select * from emp where sal >=any(select sal from emp where comm is not null);
select * from emp where sal <>all(select sal from emp where comm is not null);
select * from emp where sal >all(select sal from emp where comm is not null);
select * from emp where sal >=all(select sal from emp where comm is not null);
select * from emp where sal <all(select sal from emp where comm is not null);
select * from emp where sal <=all(select sal from emp where comm is not null);
注意看这几句的关系
链接查询:
select * from emp, dept; 产生笛卡儿积
内链接:等值链接、不等值链接
等值链接:链接中使用“=”(等号) 链接两个条件列表
select * from emp e, dept d where e.deptno=d.deptno; select * from emp e inner join dept d on e.deptno=d.deptno; //功能相等 ps:inner能够省略,系统自动识别为内链接
不等值链接:链接时使用<、 > 、>=、 <=、 between ……and ……、in等链接两个条件列表
自链接:把自身表的一个引用做为另外一个表来处理
select e.empno 雇员编号, e.ename 雇员姓名,m.empno 领导编号 from emp e, emp m where e.mgr=m.empno;
外链接:左外链接、右外链接、全外链接
左外链接:返回结果不单单符合链接条件的行记录,左表所有记录都会包含,右表不知足的用NULL填充
右外链接:返回结果不单单符合链接条件的行记录,右表所有记录都会包含,左表不知足的用NULL填充
全外链接:不管是否成功匹配,左右表记录都返回,不知足用NULL填充
oracle使用外链接有一种特殊的方法,用(+)表示外链接,放在非主表的一方
举几个栗子:
dept是左表,采用左外链接
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d ,(select empno, ename, job ,sal ,deptno from emp where comm is null) a where d.deptno=a.deptno(+);
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d left join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;
以上两个查询语句相等
采用右外链接:
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d right join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d , (select empno, ename, job ,sal ,deptno from emp where comm is null) a where d.deptno(+)=a.deptno;
以上两个查询语句相等
采用全外链接:
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d full join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;
献给和我同样的小白,有关查询还有不少知识点,这里只写了经常使用的,若有错误请指出,谢谢!