常用SQL语法文档格式.docx
- 文档编号:5133932
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:10
- 大小:17.25KB
常用SQL语法文档格式.docx
《常用SQL语法文档格式.docx》由会员分享,可在线阅读,更多相关《常用SQL语法文档格式.docx(10页珍藏版)》请在冰点文库上搜索。
selectid,name,s.id,s.namefromstudents;
--column列运算
select(age+id)colfromstudent;
selects.name+'
-'
+c.namefromclassesc,studentswheres.cid=c.id;
--where条件
select*fromstudentwhereid=2;
select*fromstudentwhereid>
7;
select*fromstudentwhereid<
3;
>
=3;
=5;
select*fromstudentwhereid!
<
5;
--and并且
2andsex=1;
--or或者
select*fromstudentwhereid=2orsex=1;
--between...and...相当于并且
select*fromstudentwhereidbetween2and5;
select*fromstudentwhereidnotbetween2and5;
--like模糊查询
select*fromstudentwherenamelike
'
%a'
;
select*fromstudentwherenamenotlike
%a%'
select*fromstudentwherenamelike'
ja%'
--in子查询
select*fromstudentwhereidin(1,2);
--notin不在其中
select*fromstudentwhereidnotin(1,2);
--isnull是空
select*fromstudentwhereageisnull;
--isnotnull不为空
select*fromstudentwhereageisnotnull;
--orderby排序
select*fromstudentorderbyname;
select*fromstudentorderbynamedesc;
select*fromstudentorderbynameasc;
--groupby分组
按照年龄进行分组统计
selectcount(age),agefromstudentgroupbyage;
按照性别进行分组统计
selectcount(*),sexfromstudentgroupbysex;
按照年龄和性别组合分组统计,并排序
selectcount(*),sexfromstudentgroupbysex,ageorderbyage;
按照性别分组,并且是id大于2的记录最后按照性别排序
selectcount(*),sexfromstudentwhereid>
2groupbysexorderbysex;
查询id大于2的数据,并完成运算后的结果进行分组和排序
selectcount(*),(sex*id)newfromstudentwhereid>
2groupbysex*idorderbysex*id;
--having分组过滤条件
按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息
selectcount(*),agefromstudentgroupbyagehavingageisnotnull;
按照年龄和cid组合分组,过滤条件是cid大于1的记录
selectcount(*),cid,sexfromstudentgroupbycid,sexhavingcid>
1;
按照年龄分组,过滤条件是分组后的记录条数大于等于2
selectcount(*),agefromstudentgroupbyagehavingcount(age)>
=2;
按照cid和性别组合分组,过滤条件是cid大于1,cid的最大值大于2
1andmax(cid)>
2;
二、子查询(嵌入到其他sql语句中的select语句,也叫嵌套查询)
1、单行子查询
SELECTenameFROMempWHEREdeptno=(SELECTdeptnoFROMempwhereename='
SMITH'
);
查询表中与smith同部门的人员名字。
因为返回结果只有一行,所以用“=”连接子查询语句
2、多行子查询
SELECTename,job,sal,deptnofromempWHEREjobIN(SELECTDISTINCTjobFROMempWHEREdeptno=10);
查询表中与部门号为10的工作相同的员工的姓名、工作、薪水、部门号。
因为返回结果有多行,所以用“IN”连接子查询语句。
3、使用ALL
SELECTename,sal,deptnoFROMempWHEREsal>
ALL(SELECTsalFROMempWHEREdeptno=30);
或
(SELECTMAX(sal)FROMempWHEREdeptno=30);
查询工资比部门号为30号的所有员工工资都高的员工的姓名、薪水和部门号。
以上两个语句在功能上是一样的,但执行效率上,函数会高得多。
4、使用ANY
ANY(SELECTsalFROMempWHEREdeptno=30);
(SELECTMIN(sal)FROMempWHEREdeptno=30);
查询工资比部门号为30号的任意一个员工工资高(只要比某一员工工资高即可)的员工的姓名、薪水和部门号。
以上两个语句在功能上是一样的,但执行效率上,函数会高得多。
5、多列子查询
SELECT*FROMempWHERE(job,deptno)=(SELECTjob,deptnoFROMempWHEREename='
6、在from子句中使用子查询
SELECTemp.deptno,emp.ename,emp.sal,t_avgsal.avgsalFROMemp,(SELECTemp.deptno,avg(emp.sal)avgsalFROMempGROUPBYemp.deptno)t_avgsalwhereemp.deptno=t_avgsal.deptnoANDemp.sal>
t_avgsal.avgsalORDERBYemp.deptno;
7、分页查询
数据库的每行数据都有一个对应的行号,称为rownum.
SELECTa2.*FROM(SELECTa1.*,ROWNUMrnFROM(SELECT*FROMempORDERBYsal)a1WHEREROWNUM<
=10)a2WHERErn>
=6;
指定查询列、查询结果排序等,都只需要修改最里层的子查询即可。
8、用查询结果创建新表
CREATETABLEmytable(id,name,sal,job,deptno)ASSELECTempno,ename,sal,job,deptnoFROMemp;
9、合并查询(union并集,intersect交集,unionall并集+交集,
minus差集)
SELECTename,sal,jobFROMempWHEREsal>
2500UNION(INTERSECT/UNIONALL/MINUS)SELECTename,sal,jobFROMempWHEREjob='
MANAGER'
合并查询的执行效率远高于and,or等逻辑查询。
10、使用子查询插入数据
CREATETABLEmyEmp(empIDnumber(4),namevarchar2(20),salnumber(6),jobvarchar2(10),deptnumber
(2));
先建一张空表;
INSERTINTOmyEmp(empID,name,sal,job,dept)SELECTempno,ename,sal,job,deptnoFROMempWHEREdeptno=10;
再将emp表中部门号为10的数据插入到新表myEmp中,实现数据的批量查询。
11、使用了查询更新表中的数据
UPDATEempSET(job,sal,comm)=(SELECTjob,sal,commFROMempwhereename='
)WHEREename='
SCOTT'
三、聚合查询
1、distinct去掉重复数据
selectcount(sex),count(distinctsex)fromstudent;
2、compute和computeby汇总查询
对年龄大于的进行汇总
selectagefromstudent
whereage>
20orderbyagecomputesum(age)byage;
对年龄大于的按照性别进行分组汇总年龄信息
selectid,sex,agefromstudent
20orderbysex,agecomputesum(age)bysex;
按照年龄分组汇总
20orderbyage,idcomputesum(age);
按照年龄分组,年龄汇总,id找最大值
selectid,agefromstudent
20orderbyagecomputesum(age),max(id);
compute进行汇总前面是查询的结果,后面一条结果集就是汇总的信息。
compute子句中可以添加多个汇总表达式,可以添加的信息如下:
a、可选by关键字。
它是每一列计算指定的行聚合
b、行聚合函数名称。
包括sum、avg、min、max、count等
c、要对其执行聚合函数的列
computeby适合做先分组后汇总的业务。
computeby后面的列一定要是orderby中出现的列。
3、cube汇总
cube汇总和compute效果类似,但语法较简洁,而且返回的是一个结果集。
selectcount(*),sexfromstudentgroupbysexwithcube;
selectcount(*),age,sum(age)fromstudentwhereageisnotnullgroupbyagewithcube;
cube要结合groupby语句完成分组汇总
四、集合运算
操作两组查询结果,进行交集、并集、减集运算
1、union和unionall进行并集运算
--union并集、不重复
selectid,namefromstudentwherenamelike'
union
selectid,namefromstudentwhereid=4;
--并集、重复
unionall
2、intersect进行交集运算
--交集(相同部分)
intersect
3、except进行减集运算
--减集(除相同部分)
except
jas%'
五、连接查询
1、简化连接查询
--简化联接查询
selects.id,s.name,c.id,c.namefromstudents,classescwheres.cid=c.id;
2、leftjoin左连接
--左连接
selects.id,s.name,c.id,c.namefromstudentsleftjoinclassescons.cid=c.id;
3、rightjoin右连接
--右连接
selects.id,s.name,c.id,c.namefromstudentsrightjoinclassescons.cid=c.id;
4、innerjoin内连接
--内连接
selects.id,s.name,c.id,c.namefromstudentsinnerjoinclassescons.cid=c.id;
--inner可以省略
selects.id,s.name,c.id,c.namefromstudentsjoinclassescons.cid=c.id;
5、crossjoin交叉连接
--交叉联接查询,结果是一个笛卡儿乘积
selects.id,s.name,c.id,c.namefromstudentscrossjoinclassesc
--wheres.cid=c.id;
6、自连接(同一张表进行连接查询)
--自连接
selectdistincts.*fromstudents,students1wheres.id<
s1.idands.sex=s1.sex;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常用 SQL 语法