首页 > 代码库 > sql笔试题-1

sql笔试题-1

技术分享

在oracle下sql:比较巧妙地是group by 部分

1 select max(c.team) TEAM,min(c.y) B,max(c.y)+1 E
2 from
3 (select a.team,b.y from nba a,nba b
4 where a.team=b.team and a.y-b.y=1) c
5 group by (c.y-rownum)
6 order by B

 

在mysql下的sql:由于mysql没有rownum所以用了它的替代

1 SELECT  cc.team TEAM, MIN(cc.y) BEGIN,MAX(cc.y)+1 END  FROM 
2 (SELECT aa.team ,aa.y ,  @rownum:=@rownum+1 AS rownum FROM 
3     (SELECT @rownum:=0) r ,
4     (SELECT team ,Y FROM `nba` ORDER BY Y) aa ,
5     (SELECT team ,Y FROM NBA ORDER BY Y) bb 
6 WHERE aa.team = bb.team AND aa.y=bb.y-1) cc 
7 GROUP BY (cc.y-cc.rownum)
8 ORDER BY MIN(cc.y)

结果:

技术分享

 

sql笔试题-1