下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据库菜鸟不可不看简单 SQL 语句小结为了大家更容易理解我举出的 SQL 语句,本文假定已经建立了一个学生成绩管 理数据库,全文均以学生成绩的管理为例来描述。网络 SQL 数据库网站空间申请: 在查询结果中显示列名:a. 用 as 关键字:select name as 姓名from students order by ageb. 直接表示:select name 姓名from students order by age2. 精确查找:a. 用 in 限定范围:select * from students where native in (湖南,四川b. betwee n.a nd :sele
2、ct * from stude nts where age betwee n 20 and 30c.“ =” :select * from students where nan 李山d. like:select * from students where name like 李 %(注意查询条件中有“ % 则说明是部分匹配,而且还 有先后信息在里面,即查找以 李”开头的匹配项。所以若查 询有 李”的所有对象,应该命令:李%;若是第二个字为李,则应为李或李或李_。e. 匹配检查符:select * from courses where cno like AC%(表示或的关系,与 in(.类似,而
3、且可 以表示范围,如:select * from courses where cno like A-C%3. 对于时间类型变量的处理a.smalldatetime 直接按照字符串处理的方式进行处理例如:select * from students where birth = 1980-1-1 and birth = 1980-12-314.集函数a. count(求和,如:select count(* from students 求学生总人数b. avg(列 求平均,女口:select avg(mark from grades where eno=?B2?c. max(列和 min(列,求最大
4、与最小5.分组 group常用于统计时,如分组查总数:select gen der,co un t(s nofrom stude ntsgroup by gen der(查看男女学生各有多少注意:从哪种角度分组就从哪列group by对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数,那么分组规则有:届别(grade 专业(mno 和性别(gender 所以有group by grade, mno,gen derselect grade, mno, gen der, coun t(*from stude ntsgroup by grade, mno, gen der通常 gro
5、up 还和 having 联用,比如查询 1 门课以上不及格的学生,则按学号(sno分类有:select sno ,co un t(* from gradeswhere mark16. UNION 联合合并查询结果,如:SELECT * FROM stude ntsWHERE name like ,张 %?UNION ALLSELECT * FROM stude ntsWHERE name like ,李 %?7. 多表查询a. 内连接select g.s no,s.n ame,c.course namefrom grades g JOIN stude nts s ON g.s no=s.s
6、noJOIN courses c ON g.c no=c.c no(注意可以引用别名b. 外连接b1.左连接 select courses.c no, max(course name,co un t(s no from coursesLEFT JOIN grades ON courses.c no=grades.c no group by courses.c no左连接特点:显示全部左边表中的所有项目,即使其中有些项中的数据未填写完全。左外连接返回那些存在于左表而右表中却没有的行,再加上内连接的行。b2.右连接与左连接类似b3.全连接select sno,n ame,majorfrom stu
7、de nts FULL JOIN majors ON stude nts. mno=majors, mno两边表中的内容全部显示c. 自身连接select cl.c no ,c1.course name,c1.p no, c2.course namefrom courses c1,courses c2 where cl.p no=c2.c no采用别名解决问题。d. 交叉连接select last name+first name from last name CROSS JOIN firsta nme相当于做笛卡儿积8.嵌套查询 a.用关键字 IN,如查询李山的同乡select * from
8、stude ntswhere native in (select native from students where name 二二李山 ?b.使用关键字 EXIST,比如,下面两句是等价的:select * from stude ntswhere sno in (select sno from grades where eno=?B2?select * from stude nts where exists(select * from grades wheregrades.s no=stude nts.s no AND eno=?B2?9. 关于排序 ordera. 对于排序 order,
9、有两种方法:asc 升序和 desc 降序b. 对于排序 order,可以按照查询条件中的某项排列,而且这项可用数字表示,如:select sno ,co un t(* ,avg(mark from gradesgroup by snohav ing avg(mark85order by 310. 其他a. 对于有空格的识别名称,应该用括住。b.对于某列中没有数据的特定查询可以用null 判断,如 select sn o,course no from grades where mark IS NULLc. 注意区分在嵌套查询中使用的 any 与 all 的区别,any 相当于逻辑运算“|而all 则相当于逻辑运算“ &d. 注意在做否定意义的查询是小心进入陷阱:如,没有选修,B2?课程的学生:select stude nts.*from stude nts, gradeswhere stude nts.s no=grades.s noAND o ?B2?上面的查询方式是错误的,正确方式见下方:select * from stude ntswhere not exists (select * from gradeswhere grades.s no=stude nts.s no AND eno=B211. 关于有难度多重嵌套查询的解决思
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 13177-2026核电厂优先电源
- 2025年无锡市江阴市教育系统招聘笔试真题
- 2025年呼伦贝尔市额尔古纳市招聘专职消防救援队员笔试真题
- 主体结构施工方案难点(3篇)
- 设备监理师《设备监理验收标准》专项练习(完整版)
- 小学思想政治理论课情况调研报告2026(3篇)
- 2026年山西小升初数学考试(真题)及参考答案
- 2026年青海小升初(英语)考试试卷(真题)(含答案)
- 2026年山西(专升本)护理学真题试卷附答案
- 2026年山东中考数学真题(含答案)
- 畜牧统计培训课件
- Unit 3 第4课时Fuel up教学设计
- 美食表演秀创新创业项目商业计划书
- 2025年山东省公务员录用考试《行测》真题及答案
- 乡镇医院标准化建设课件
- GA/T 2183-2024法庭科学足迹检验实验室建设规范
- 2024年度移动通信基站维护服务合同范本3篇
- 建筑中级职称《建筑电气工程》历年考试真题题库(含答案)
- 医院培训课件:《静脉血栓栓塞症(VTE)专题培训》
- T-CI 263-2024 水上装配式钢结构栈桥(平台)施工技术规程
- TZJXDC 002-2022 电动摩托车和电动轻便摩托车用阀控式铅酸蓄电池
评论
0/150
提交评论