好文档 - 专业文书写作范文服务资料分享网站

Java面试题(SSH框架) 

天下 分享 时间: 加入收藏 我要投稿 点赞

);

4.统计每年每月的信息yearmonthamount199111.1199121.2199131.3199141.4199212.1199222.2199232.3199242.4查成这样一个结果

yearm1m2m3m419911.11.21.31.419922.12.22.32.4

提示:这个与工资条非常类似,与学生的科目成绩也很相似。

准备sql语句:

droptableifexistssales;

createtablesales(idintauto_incrementprimarykey,yearvarchar(10),monthvarchar(10),amountfloat(2,1));

insertintosalesvalues(null,'1991','1',1.1),(null,'1991','2',1.2),(null,'1991','3',1.3),(null,'1991','4',1.4),(null,'1992','1',2.1),(null,'1992','2',2.2),(null,'1992','3',2.3),(null,'1992','4',2.4);

答案一、

selectsales.year,

(selectt.amountfromsalestwheret.month='1'andt.year=sales.year)'1',(selectt.amountfromsalestwheret.month='1'andt.year=sales.year)'2',(selectt.amountfromsalestwheret.month='1'andt.year=sales.year)'3',(selectt.amountfromsalestwheret.month='1'andt.year=sales.year)as'4'fromsalesgroupbyyear;

5.显示文章标题,发帖人、最后回复时间表:id,title,postuser,postdate,parentid准备sql语句:

droptableifexistsarticles;

createtablearticles(idintauto_incrementprimarykey,titlevarchar(50),postuservarchar(10),postdatedatetime,parentidintreferencesarticles(id));insertintoarticlesvalues

(null,'第一条','张三','1998-10-1012:32:32',null),(null,'第二条','张三','1998-10-1012:34:32',null),(null,'第一条回复1','李四','1998-10-1012:35:32',1),(null,'第二条回复1','李四','1998-10-1012:36:32',2),(null,'第一条回复2','王五','1998-10-1012:37:32',1),(null,'第一条回复3','李四','1998-10-1012:38:32',1),(null,'第二条回复2','李四','1998-10-1012:39:32',2),(null,'第一条回复4','王五','1998-10-1012:39:40',1);

答案:

selecta.title,a.postuser,

(selectmax(postdate)fromarticleswhereparentid=a.id)replyfromarticlesawherea.parentidisnull;

注释:子查询可以用在选择列中,也可用于where的比较条件中,还可以用于from从句中。3.删除除了id号不同,其他都相同的学生冗余信息2.学生表如下:id号学号姓名课程编号课程名称分数12005001张三0001数学6922005002李四0001数学8932005001张三0001数学69

A:deletefromtablenamewhereid号notin(selectmin(id号)fromtablenamegroupby学号,姓名,课程编号,课程名称,分数)实验:

createtablestudent2(idintauto_incrementprimarykey,codevarchar(20),namevarchar(20));insertintostudent2values(null,'2005001','张三'),(null,'2005002','李四'),(null,'2005001','张三');//如下语句,mysql报告错误,可能删除依赖后面统计语句,而删除又导致统计语句结果不一致。

deletefromstudent2whereidnotin(selectmin(id)fromstudent2groupbyname);//但是,如下语句没有问题:

select*fromstudent2whereidnotin(selectmin(id)fromstudent2groupbyname);

//于是,我想先把分组的结果做成虚表,然后从虚表中选出结果,最后再将结果作为删除的条件数据。

deletefromstudent2whereidnotin(selectmidfrom(selectmin(id)midfromstudent2groupbyname)ast);或者:

deletefromstudent2whereidnotin(selectmin(id)from(select*fromstudent2)astgroupbyt.name);

4.航空网的几个航班查询题:表结构如下:

flight{flightID,StartCityID,endCityID,StartTime}city{cityID,CityName)实验环境:

createtablecity(cityIDintauto_incrementprimarykey,cityNamevarchar(20));createtableflight(flightIDintauto_incrementprimarykey,

StartCityIDintreferencescity(cityID),endCityIDintreferencescity(cityID),StartTimetimestamp);

//航班本来应该没有日期部分才好,但是下面的题目当中涉及到了日期insertintocityvalues(null,'北京'),(null,'上海'),(null,'广州');insertintoflightvalues

(null,1,2,'9:37:23'),(null,1,3,'9:37:23'),(null,1,2,'10:37:23'),(null,2,3,'10:37:23');

1、查询起飞城市是北京的所有航班,按到达城市的名字排序

参与运算的列是我起码能够显示出来的那些列,但最终我不一定把它们显示出来。各个表组合出来的中间结果字段中必须包含所有运算的字段。select*fromflightf,cityc

wheref.endcityid=c.cityidandstartcityid=

(selectc1.cityidfromcityc1wherec1.cityname=\北京\orderbyc.citynameasc;

mysql>selectflight.flightid,'北京'startcity,e.citynamefromflight,cityewhereflight.endcityid=e.cityidandflight.startcityid=(selectcityidfromcitywherecityname='北京');

mysql>selectflight.flightid,s.cityname,e.citynamefromflight,citys,cityewhereflight.startcityid=s.cityidands.cityname='北京'andflight.endCityId=e.cityIDorderbye.cityNamedesc;

2、查询北京到上海的所有航班纪录(起飞城市,到达城市,起飞时间,航班号)selectc1.CityName,c2.CityName,f.StartTime,f.flightIDfromcityc1,cityc2,flightfwheref.StartCityID=c1.cityIDandf.endCityID=c2.cityIDandc1.cityName='北京'andc2.cityName='上海'

3、查询具体某一天(2005-5-8)的北京到上海的的航班次数

selectcount(*)from

(selectc1.CityName,c2.CityName,f.StartTime,f.flightIDfromcityc1,cityc2,flightfwheref.StartCityID=c1.cityIDandf.endCityID=c2.cityIDandc1.cityName='北京'andc2.cityName='上海'

and查帮助获得的某个日期处理函数(startTime)like'2005-5-8%'mysql中提取日期部分进行比较的示例代码如下:

select*fromflightwheredate_format(starttime,'%Y-%m-%d')='1998-01-02'5.查出比经理薪水还高的员工信息:Droptableifnotexistsemployees;

createtableemployees(idintprimarykeyauto_increment,namevarchar(50),salaryint,manageridintreferencesemployees(id));

insertintoemployeesvalues(null,'lhm',10000,null),(null,'zxx',15000,1),(null,'flx',9000,1),(null,'tg',10000,2),(null,'wzg',10000,3);Wzg大于flx,lhm大于zxx

解题思路:

根据sql语句的查询特点,是逐行进行运算,不可能两行同时参与运算。

涉及了员工薪水和经理薪水,所有,一行记录要同时包含两个薪水,所有想到要把这个表自关联组合一下。

首先要组合出一个包含有各个员工及该员工的经理信息的长记录,譬如,左半部分是员工,右半部分是经理。而迪卡尔积会组合出很多垃圾信息,先去除这些垃圾信息。selecte.*fromemployeese,employeesmwheree.managerid=m.idande.salary>m.salary;

6、求出小于45岁的各个老师所带的大于12岁的学生人数数据库中有3个表teacher表,student表,tea_stu关系表。teacher表teaIDnameagestudent表stuIDnameageteacher_student表teaIDstuID要求用一条sql查询出这样的结果

1.显示的字段要有老师name,age每个老师所带的学生人数2只列出老师age为40以下,学生age为12以上的记录预备知识:

1.sql语句是对每一条记录依次处理,条件为真则执行动作(select,insert,delete,update)2.只要是迪卡尔积,就会产生“垃圾”信息,所以,只要迪卡尔积了,我们首先就

要想到清除“垃圾”信息实验准备:

droptableifexiststea_stu;droptableifexiststeacher;

droptableifexistsstudent;

createtableteacher(teaIDintprimarykey,namevarchar(50),ageint);createtablestudent(stuIDintprimarykey,namevarchar(50),ageint);createtabletea_stu(teaIDintreferencesteacher(teaID),stuID

student(stuID));

insertintoteachervalues(1,'zxx',45),(2,'lhm',25),(3,'wzg',26),(4,'tg',27);insertintostudentvalues(1,'wy',11),(2,'dh',25),(3,'ysq',26),(4,'mxc',27);insertintotea_stuvalues(1,1),(1,2),(1,3);insertintotea_stuvalues(2,2),(2,3),(2,4);insertintotea_stuvalues(3,3),(3,4),(3,1);

insertintotea_stuvalues(4,4),(4,1),(4,2),(4,3);结果:2?3,3?2,4?3

intreferences

解题思路:(真实面试答题时,也要写出每个分析步骤,如果纸张不够,就找别人要)1要会统计分组信息,统计信息放在中间表中:selectteaid,count(*)fromtea_stugroupbyteaid;

2接着其实应该是筛除掉小于12岁的学生,然后再进行统计,中间表必须与student关联才能得到12岁以下学生和把该学生记录从中间表中剔除,代码是:selecttea_stu.teaid,count(*)totalfromstudent,tea_stu

wherestudent.stuid=tea_stu.stuidandstudent.age>12groupbytea_stu.teaid3.接着把上面的结果做成虚表与teacher进行关联,并筛除大于45的老师selectteacher.teaid,teacher.name,totalfromteacher,(selecttea_stu.tea

id,count(*)totalfromstudent,tea_stuwherestudent.stuid=tea_stu.stuidandstudent.age>12groupbytea_stu.teaid)astea_stu2whereteacher.teaid=tea_stu2.teaidandteacher.age<45;

7.求出发帖最多的人:

selectauthorid,count(*)totalfromarticlesgroupbyauthoridhavingtotal=

(selectmax(total2)from(selectcount(*)total2fromarticlesgroupbyauthorid)ast);selectt.authorid,max(t.total)from

(selectauthorid,count(*)totalfromarticles)ast

这条语句不行,因为max只有一列,不能与其他列混淆。selectauthorid,count(*)totalfromarticles

groupbyauthoridhavingtotal=max(total)也不行。

10、一个用户表中有一个积分字段,假如数据库中有100多万个用户,若要在每年第一天凌

Java面试题(SSH框架) 

);4.统计每年每月的信息yearmonthamount199111.1199121.2199131.3199141.4199212.1199222.2199232.3199242.4查成这样一个结果yearm1m2m3m419911.11.21.31.419922.12.22.32.4提示:这个与工资条非常类似,与学生的科目成绩也很相似。<
推荐度:
点击下载文档文档为doc格式
5lk8e107a235m4y30uut
领取福利

微信扫码领取福利

微信扫码分享