SQL数据库中高级查询 表连接与子查询

--高级查询


--模糊查询
--通配符 1.  _  A like 'c_'(以c开头的两个字符)  2.  %   B like 'co_%'(以co开头的至少三个字符)
--         3. []  C like '9w0[1-2]'(范围是1到2)   4.  ^   D like '%[A-D][^1-2]'(^取补集)
--         5. []  C like '9w0[1,2]'(1和2)
--like 
--select *from stuInfo(表) where stuName(列) like '李%' --查找所有姓李的 
-- is null
--select *from stuInfo where stuAddress is null --查找stuAddress为空的内容
-- between... and 数值 时间 日期
--select *from stuInfo where stuAge between 18 and 28
-- in 
--select *from stuInfo where stuSex in ('男','女')
--select *from stuInfo where stuSex='男' or stuSex='女' (比上一个快)




--聚合函数
 -- sum 求和
--select sum(writtenExam) as 'sumScore' from  stuMarks (求所有笔试成绩的总分)
   --select sum(writtenExam+LabExam) as 'Big_sumScore' from  stuMarks
          -- avg 平均数
   --select avg(WrittenExam) as 'avg' from stuMarks
--select avg(distinct WrittenExam) as 'avg' from stuMarks (帮相同的数据删掉在求平均数)
--select avg(WrittenExam) as 'avg' from stuMarks where stuNo in ('s25303','s25302')
          -- count 满足条件的数据条数
   --select count(*) from stuInfo
--select count(distinct stuName) from stuInfo
 -- max min
 
--分组查询(常与聚合函数一起使用)
 --select *from score as avgScore
--分组依据
--select AVG(score) from score group by courseId 
--select courseId,AVG(score) from score group by courseId
--having (分组之后在条件)
 --select courseId,AVG(score) from score group by courseId  having AVG(score)>80
--having 与 where 的区别 
-- having必须跟在group by 的后面
--where的用法 可以单独使用 不用group by 在分组前求条件
--select className,avg(score) as 'AVG' from table_test where sex='男' group by className
-- where和having 同时存在
 --select className,avg(score) as 'AVG' from table_test where sex='男' group by className having avg(score)>80

--表连接 所查询的数据来自于多张表
--内链接 连接的时候找桥梁 实际上是主外键的关系  inner join...in...
--select *from student inner join score on student.stuNo=score.studentId
--select  student.stuName,score.courseId,score.score From student inner join score on student.stuNo=score.studentId
--select  student.stuName,score.courseId,score.score From student s inner join score c on s.stuNo=c.studentId
--select  student.stuName,score.co.cname,score.score From student s inner join score c on s.stuNo=c.studentId 
-- inner join course co on c.courseId=co.cid
--外连接 有一张表必须出现在最后的结果
--左外连接区别在于以哪个表为主
--左外连接
--select *from student left join score n student.stuNo=score.studentId
--select student.stuName,Course.cname,score.score from student left join score on student.stuNo=score.studentId
-- left join Course on score.courseId=Course.cid
--右外连接  
--select *from student right join score n student.stuNo=score.studentId
--交叉连接 笛卡尔链接 第一个表m行 第二个表n行  则返回的笛卡儿积的表有m*n行 但经常是毫无意义的
--select *from course cross join score

 --变形的写法 不同表中,列的名字都不一样就可以省略表的名字,直接写列
 --select stuName,cname,score from student,score,Course where stuNo=studentId and courseId=cid

--子查询  一个查询里面在嵌套一个查询 如果能使用表连接则优先使用 表连接比子查询效率高
--分布解决问题  先找赵六的年龄 在判断比赵六大的
 --select stuAge from stuInfo where  stuName='赵六'
--select *from  stuInfo where stuAge>(select stuAge from stuInfo where  stuName='赵六')
--update stuInfo set stuSeat=10 where stuAge>(select stuAge from stuInfo where  stuName='赵六')

  --In 的子查询
  --select *from  stuInfo where stuAge in (select stuAge from stuInfo where  stuName='赵六') --括号的结果可以是多个


--运用
--查询pubs数据库中titles表价格在10-20美元之间且种类(type)为business或popular_comp
--select *from titles where price between 10 and 20 and (type='business' or type='popular_comp')
--查询pubs数据库中每个作者的编号,姓名所出的书的编号,并对结果排序

--select a.au_id as 编号,au_fname+'.'+au_lname as 姓名,ta.title_id as 书编号 from authors inner join titleauthor ta on a.au_id=ta.au_id







--------携着一股什么也不服的劲在活着


猜你喜欢

转载自blog.csdn.net/zoweiccc/article/details/77816124