2022年SQL的简单查询实例教程 .pdf

上传人:C****o 文档编号:33405277 上传时间:2022-08-10 格式:PDF 页数:5 大小:81.87KB
返回 下载 相关 举报
2022年SQL的简单查询实例教程 .pdf_第1页
第1页 / 共5页
2022年SQL的简单查询实例教程 .pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述

《2022年SQL的简单查询实例教程 .pdf》由会员分享,可在线阅读,更多相关《2022年SQL的简单查询实例教程 .pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、 SQL 的简单查询实例教程关键词 :菜鸟学数据库之简单SQL 语句小结为了大家更容易理解我举出的SQL 语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。1. 在查询结果中显示列名:a. 用 as 关键字: select name as 姓名 from students order by age b. 直接表示: select name 姓名 from students order by age 2. 精确查找 : a. 用 in 限定范围: select * from students where native in (湖南 , 四川 ) b.between

2、.and:select * from students where age between 20 and 30 c. “=” :select * from students where name = 李山 d.like:select * from students where name like 李% ( 注意查询条件中有“% ” ,则说明是部分匹配,而且还有先后信息在里面,即查找以 “ 李” 开头的匹配项。 所以若查询有“ 李” 的所有对象, 应该命令: % 李%; 若是第二个字为李,则应为 _ 李% 或_ 李或_ 李_ 。) e. 匹配检查符: select * from courses

3、where cno like AC% (表示或的关系,与in(.)类似,而且 可以表示范围,如:select * from courses where cno like A-C%) 3. 对于时间类型变量的处理a.smalldatetime:直接按照字符串处理的方式进行处理,例如:select * from students where birth = 1980-1-1 and birth = 1980-12-31 4. 集函数a.count()求和,如: select count(*) from students (求学生总人数 ) b.avg(列) 求平均,如:select avg(mar

4、k) from grades where cno=?B2?c.max(列)和 min( 列) ,求最大与最小5. 分组 group 常用于统计时,如分组查总数:select gender,count(sno) from students 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - group by gender ( 查看男女学生各有多少) 注意:从哪种角度分组就从哪列group by 对于多重分组,只需将分组规则罗列。比如查

5、询各届各专业的男女同学人数,那么分组规则有:届别(grade)、专业 (mno)和性别 (gender),所以有 group by grade, mno, gender select grade, mno, gender, count(*) from students group by grade, mno, gender 通常 group还和 having联用,比如查询1 门课以上不及格的学生,则按学号(sno)分类有:select sno,count(*) from grades where mark1 6.UNION联合合并查询结果,如:SELECT * FROM students WH

6、ERE name like ,张%?UNION ALL SELECT * FROM students WHERE name like ,李%?7. 多表查询a. 内连接select g.sno,s.name,c.coursename from grades g JOIN students s ON g.sno=s.sno JOIN courses c ON o=o ( 注意可以引用别名) b. 外连接b1. 左连接select o,max(coursename),count(sno) from courses LEFT JOIN grades ON o=o group by o 左连接特点:显

7、示全部左边表中的所有项目,即使其中有些项中的数据未填写完全。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - 左外连接返回那些存在于左表而右表中却没有的行,再加上内连接的行。b2. 右连接与左连接类似b3. 全连接select sno,name,major from students FULL JOIN majors ON students.mno=majors.mno 两边表中的内容全部显示c. 自身连接select o,c1.

8、coursename,c1.pno,c2.coursename from courses c1,courses c2 where c1.pno=o 采用别名解决问题。d. 交叉连接select lastname+firstname from lastname CROSS JOIN firstanme 相当于做笛卡儿积8. 嵌套查询a. 用关键字 IN, 如查询李山的同乡:select * from students where native in (select native from students where name=? 李山 ?) b. 使用关键字EXIST, 比如,下面两句是等价的

9、:select * from students where sno in (select sno from grades where cno=?B2?)select * from students where exists (select * from grades where grades.sno=students.sno AND cno=?B2?)9. 关于排序 order a. 对于排序order ,有两种方法:asc 升序和 desc 降序名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - -

10、- - 第 3 页,共 5 页 - - - - - - - - - b. 对于排序 order, 可以按照查询条件中的某项排列,而且这项可用数字表示,如:select sno,count(*) ,avg(mark) from grades group by sno having avg(mark)85 order by 3 10. 其他a. 对于有空格的识别名称,应该用括住。b. 对于某列中没有数据的特定查询可以用null判断,如select sno,courseno from grades where mark IS NULL c. 注意区分在嵌套查询中使用的any 与 all 的区别, a

11、ny 相当于逻辑运算“| ”而 all 则相当于逻辑运算“& ”d. 注意在做否定意义的查询是小心进入陷阱:如,没有选修,B2?课程的学生:select students.* from students, grades where students.sno=grades.sno AND o ?B2? 上面的查询方式是错误的,正确方式见下方:select * from students where not exists (select * from grades where grades.sno=students.sno AND cno=B2) 11. 关于有难度多重嵌套查询的解决思想:如,选修

12、了全部课程的学生:select * from students where not exists ( select * from courses where NOT EXISTS (select * from grades where sno=students.sno AND cno=o) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - 最外一重: 从学生表中选,排除那些有课没选的。用 not exist。由于讨论对象是课程,所以第二重查询从course表中找,排除那些选了课的即可。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁