🎈个人主页:🎈 :✨✨✨初阶牛✨✨✨
🐻推荐专栏: 🍔🍟🌯 c语言初阶
🔑个人信条: 🌵知行合一
🍉本篇简介:>:上一篇学习了如何使用SQL语句进行简单的数据查询,本篇记录一些在简单查询的基础上稍微复杂一点的查询,希望对大家有帮助.
本篇可当做例题练习,
1.查询比”林红”年纪大的男学生信息
语句:
select *
from Student
where Sex='男' and
year(Birth)-(select year(Birth)from Student--这里是需要告诉查询的表名,相当于嵌套
where Sname='林红')<0
1.检索所有学生的选课信息,包括学号、姓名、课程名、成绩,性别.
语句:
select sc.sno,sname, course.Cno,Cname,Grade,Sex
--这里如果两个表中都有同一个属性,则需要标明在哪个表,如sc.sno
from student,sc,Course
where student.Sno=sc.Sno and Sc.Cno=course.Cno
3.查询已经选课的学生的学号、姓名、课程名、成绩.
语句:
select sc.sno ,sname , Cname , Grade
from student s , course c, sc
where s.sno=sc.sno and c.cno=sc.cno
(4)查询选修了“C语言程序设计”的学生的学号与姓名
–a.用内连接查询
语句:
select sc.Sno,sname from student inner join sc on
student.Sno=sc.Sno inner join course on sc.Cno =course.cno
and Cname='C语言程序设计'
–b.用连接查询
语句:
select sc.Sno,sname from student,sc,course where
student .Sno=sc.Sno and sc.Cno =course.cno
and Cname='C语言程序设计'
–c.用子查询
语句:
select Sno,sname from student where Sno in
(select Sno from sc where Cno=
(select cno from course where Cname ='C语言程序设计'))
(5)查询与”张虹”在同一个班级的学生学号、姓名、家庭住址
–a.用连接查询
语句:
select a.Sno,a.sname,a.Home_addr from student a,student b
where a.Classno =b.Classno and b.Sname ='张虹' and a.Sname!='张虹'
–b.用子查询
语句:
select Sno,sname,Home_addr from student where
classno=(select classno from student where sname='张虹')
and sname!='张虹'
(6)查询其他班级中比”051”班所有学生年龄大的学生的学号、姓名
代码1:
select Sno,sname,Home_addr from student where
classno!='051' and Birth<all (select Birth from student where classno='051')
代码2:
select Sno,sname,Home_addr from student where
classno!='051' and Birth<(select min(Birth) from student where classno='051')
(7)(选作)查询选修了全部课程的学生姓名。本题使用除运算的方法。
–由题意可得另一种语言,没有一个选了课的学生没有选course表里的课程。那么,我们需要两个NOT EXISTS表示双重否定;
语句:
select Sname from student
where not exists (
select * from course
where not exists (
select * from sc
where sno=student. sno
and cno=Course.cno))
(8)查询至少选修了学生“20110002”选修的全部课程的学生的学号,姓名。
语句:
select Sno, Sname from student
where sno in (
select distinct sno from sc as sc1
where not exists (
select * from sc as sc2 where sc2.sno='20110002'
and not exists (
select * from sc as sc3 where sc3.Sno=sc1.sno and
sc3.cno=sC2.cno) )
)
(9)检索选修了“高数”课且成绩至少高于选修课程号为“002"课程的学生的学号、课程号、成绩,并按成绩从高到低排列。
语句:
select sc.Sno, sc.cno , grade from sc where
grade >all(select grade from sc where cno='002' ) and
Cno= (select Cno
from course where Cname='高数')
order by Grade desc
(10)检索选修了至少3门以上课程的学生的学号、总成绩(不统计不及格的成绩),并要求按总成绩降序排列。
语句:
select sno,SUM(grade) from sc where sno in (select Sno from sc group by sno
having COUNT(*)>=3) and Grade>=60 group by sno
order by SUM (grade) desc
(12)检索多于3名学生选修的并以3结尾的课程号的平均成绩。
语句:
select avg(Grade) as 平均成绩
from sc
where Cno like '%3' group by cno
having count (Cno)>3
(13)检索最高分与最低分之差大于5分的学生的学号、姓名、最高分、最底分。
select distinct sc.sno 学号,sname 姓名,
max (grade) as最高分,min (grade) as最低分
from student,sc
where sc.sno=student.Sno group by sc.sno , Sname
having max(grade) -min (grade) >5
(14)创建一个表Student_other,结构同student,输入若干记录,部分记录和student表中的相同。
–创建过程:
create table student__other (
Sno char (8) primary key,
Sname varchar (8) not null,
sex char(2) not null,
Birth smalldatetime not null,
Classno char (3) not null,
Entrance_date smalldatetime not null,
Home_addr varchar (40) ,
sdept char (2) not null,
Postcode char (6)
)
随意插入几条student表中没有的数据:

–a.查询同时出现在Student表和student_other表中的记录
语句:
select * from student__other so ,student s
where so.sno=s.sno
----b.查询Student表和Student_other表中的全部记录
代码:
select * from student
union
select * from student__other
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI