常用SQL语句格式积累

上传人:平*** 文档编号:14746809 上传时间:2017-11-01 格式:DOCX 页数:13 大小:27.39KB
返回 下载 相关 举报
常用SQL语句格式积累_第1页
第1页 / 共13页
常用SQL语句格式积累_第2页
第2页 / 共13页
常用SQL语句格式积累_第3页
第3页 / 共13页
常用SQL语句格式积累_第4页
第4页 / 共13页
常用SQL语句格式积累_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《常用SQL语句格式积累》由会员分享,可在线阅读,更多相关《常用SQL语句格式积累(13页珍藏版)》请在金锄头文库上搜索。

1、常用 SQL语句格式积累一、常用的语句格式积累:Select top 3 * from class order by id desc 表示查询前 3条的数据并且按降来排select distinct 年龄 from class 去除重复行的查询select top 2 * from class order by newid() 表示随便查询 2行数据select * from class where 年龄=20 or 年龄=19 and 性别=男 表示先查询年龄等于19 并且是男的 在查询所有年龄等于 20的人 先执行 AND查询 再执行 ORselect * from class where

2、 id not in (select id from class1) 表示查询表一中在表2中没有的数据select * from class where id between 1 and 5 表示查询 1到 5的数据select * from class where id not between 1 and 5 表示查询不是 1到 5的数据select * from class where 姓名 like 刘% 表示查询刘开头的人select * from class where 姓名 like %丹%查询包含丹的人select * from class where 姓名 like 刘丹%查询

3、以刘或者以丹开头的数据select * from class where 姓名 like %刘丹%查询包含丹或者刘的数据select * from class where 年龄 like 1-2%查询数据不是以 1到 2之间开头的select * from class where 年龄 like 刘陈% 查询不是以刘或者陈开头的数据select * from class where 工作地址 is null 表示查询工作地址不是空值的数据select * from class where 工作地址 is not null 查询不是空植的数据select * from class where i

4、d (select avg(年龄) from class) 求年龄大于平均值的数据select * from class where 年龄= (select max(年龄) from class) 求年龄最大的、select * from class where 年龄= (select min(年龄) from class) 年龄最小的人select count(*) from class 表中数据总条数select count(年龄) from class 查询年龄不为空的select sum(年龄) from class where 性别=男 得到性别为男的的总年龄数select 年龄,c

5、ount(*) from class group by 年龄 表示对年龄进行汇总,就是说 对相同年龄的人人数进行汇总 select 年龄,count(*) from class group by 年龄 having 年龄20 对年龄大于 20的才进行汇总select 年龄,count(*) from class group by 年龄 having (年龄 in (20) 年龄在 20范围内的汇总select * from class union select * from class3 将两个表连接到一起来 删除重复的行select * from class union all select

6、 * from class3 保留重复的行select id from class intersect select id from class1 两个表相同的数据 这里因为没有两个相同的表所以只查询 ID相同的select * from class except select * from class3 两个相同的表的不相同的数据insert into class3 (姓名)values (dadad) 表示在表中的姓名列插入一个数据 只所以要写是哪个列的数据 是因为不用我插入 id了 我门一般设置 id是自动生成的 所以再这里要注明;update class3 set 工作地址=湖北 wh

7、ere id=2 对 ID=2的行 的工作地址的列进行修改update class3 set 工作地址=湖北 对所有工作地址的列进行修改delete top(1) class 删除表中前 1条的数据delete from class3 where id=2删除 ID=2的行select top 3 * from class order by newid() 随机查询 3条数据select * from class order by 名字 collate chinese_prc_cs_as 按音序查询二、常用的例子收集-Student(S#,Sname,Sage,Ssex)学生表 Course(

8、C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC whereC#=001) a,(selects#,score from SC where C#=002)b where a.scoreb.score and a.s#=b.s#;2、查询平均成绩大于 60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having

9、avg(score) 60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname) from Teacher where Tname like 李%; 5、查询没学过“叶平”老师课的同学的学号、姓名;Student.S#,Student.Sname

10、from Student where S# not in (select distinct( SC.S#) fromSC,Course,Teacher where SC.C#=Course.C# andTeacher.T#=Course.T# and Teacher.Tname=叶平); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname fromStudent,SC where Student.S#=SC.S# andSC.C#=001and exists( Select * from SC as SC_2

11、where SC_2.S#=SC.S# andSC_2.C#=002); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C#and Teacher.T#=Course.T# andTeacher.Tname=叶平 groupby S# having count(SC.C#)=(select count(C#) fromCourse,Teacher whereTeacher.T#=Course.

12、T# and Tname=叶平); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select S#,Sname from (selectStudent.S#,Student.Sname,score ,(select score from SC SC_2 whereSC_2.S#=Student.S# and SC_2.C#=002) score2 from Student,SC where Student.S#=SC.S# andC#=001) S_2 wherescore2 60); 10、查询没有学全所有课的同学的学号、姓名; select Stud

13、ent.S#,Student.Sname from Student,SC where Student.S#=SC.S# groupby Student.S#,Student.Sname having count(C#) =60 THEN 1ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC T,Course where t.C#=course.C# GROUP BY t.C# ORDER BY 100 * SUM(CASE WHEN isnull(score,0)=60 THEN 1ELSE0 END)/COUNT(*) DESC 20、查询如下课程平均成绩和及格率的百

14、分数(用1 行显示):企业管理(001),马克思(002),OO&UML(003),数据库(004) SELECT SUM(CASE WHEN C# =001 THENscore ELSE 0 END)/SUM(CASEC# WHEN 001 THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASEWHEN C# = 001 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASEWHEN C# = 001 THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASEWHEN C# = 002 THEN sco

15、re ELSE 0END)/SUM(CASEC# WHEN 002 THEN 1 ELSE 0 END) AS 马克思平均分 ,100 * SUM(CASEWHEN C# = 002 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASEWHEN C# = 002 THEN 1 ELSE 0 END) AS 马克思及格百分数 ,SUM(CASEWHEN C# = 003 THEN score ELSE 0END)/SUM(CASE C# WHEN 003 THEN 1 ELSE 0 END) AS UML平均分 ,100 * SUM(CASEWHEN C# = 0

16、03 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASEWHEN C# = 003 THEN 1 ELSE 0 END) AS UML及格百分数 ,SUM(CASEWHEN C# = 004 THEN score ELSE 0END)/SUM(CASEC# WHEN 004 THEN 1 ELSE 0 END) AS 数据库平均分 ,100* SUM(CASE WHEN C# = 004 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASEWHEN C# = 004 THEN 1 ELSE 0 END) AS 数据库及格百分数 FROM SC 21、查询不同老师所教不同课程平均分从高到低显示 SELECT max(Z.T#) AS 教师 ID,MAX(Z.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号