【正文】
m flight where startcityid = (select cityid from city where cityname=39。beijing39。)。select ,39。beijing39。, from flight where startcityid = (select cityid from city where cityname=39。beijing39。)union和union all有什么不同?假設(shè)我們有一個(gè)表Student,包括以下字段與數(shù)據(jù):drop table student。create table student(id int primary key,name nvarchar2(50) not null,score number not null)。insert into student values(1,39。Aaron39。,78)。insert into student values(2,39。Bill39。,76)。insert into student values(3,39。Cindy39。,89)。insert into student values(4,39。Damon39。,90)。insert into student values(5,39。Ella39。,73)。insert into student values(6,39。Frado39。,61)。insert into student values(7,39。Gill39。,99)。insert into student values(8,39。Hellen39。,56)。insert into student values(9,39。Ivan39。,93)。insert into student values(10,39。Jay39。,90)。mit。Union和Union All的區(qū)別。 select *from studentwhere id 4unionselect *from studentwhere id 2 and id 6結(jié)果將是1 Aaron 782 Bill 763 Cindy 894 Damon 905 Ella 73如果換成Union All連接兩個(gè)結(jié)果集,則返回結(jié)果是:1 Aaron 782 Bill 763 Cindy 893 Cindy 894 Damon 905 Ella 73可以看到,Union和Union All的區(qū)別之一在于對(duì)重復(fù)結(jié)果的處理?! NION在進(jìn)行表鏈接后會(huì)篩選掉重復(fù)的記錄,所以在表鏈接后會(huì)對(duì)所產(chǎn)生的結(jié)果集進(jìn)行排序運(yùn)算,刪除重復(fù)的記錄再返回結(jié)果。實(shí)際大部分應(yīng)用中是不會(huì)產(chǎn)生重復(fù)的記錄,最常見的是過程表與歷史表UNION。如:select * from gc_dfysunionselect * from ls_jg_dfys 這個(gè)SQL在運(yùn)行時(shí)先取出兩個(gè)表的結(jié)果,再用排序空間進(jìn)行排序刪除重復(fù)的記錄,最后返回結(jié)果集,如果表數(shù)據(jù)量大的話可能會(huì)導(dǎo)致用磁盤進(jìn)行排序。 而UNION ALL只是簡單的將兩個(gè)結(jié)果合并后就返回。這樣,如果返回的兩個(gè)結(jié)果集中有重復(fù)的數(shù)據(jù),那么返回的結(jié)果集就會(huì)包含重復(fù)的數(shù)據(jù)了?!男噬险f,UNION ALL 要比UNION快很多,所以,如果可以確認(rèn)合并的兩個(gè)結(jié)果集中不包含重復(fù)的數(shù)據(jù)的話,那么就使用UNION ALL, 取出sql表中第31到40的記錄(以自動(dòng)增長ID為主鍵)sql server方案1: select top 10 * from t where id not in (select top 30 id from t order by id ) orde by idsql server方案2: select top 10 * from t where id in (select top 40 id from t order by id) order by id descmysql方案:select * from t order by id limit 30,10oracle方案:select * from (select rownum r,* from t where r=40) where r30待整理進(jìn)去的內(nèi)容pageSize=20。pageNo = 5。(直接利用sql語句進(jìn)行分頁,效率最高和最推薦的)mysql:sql = select * from articles limit + (pageNo1)*pageSize + , + pageSize。oracle: sql = select * from + (select rownum r,* from + (select * from articles order by postime desc) + where rownum= + pageNo*pageSize +) tmp + where r + (pageNo1)*pageSize。注釋:第7行保證rownum的順序是確定的,因?yàn)閛racle的索引會(huì)造成rownum返回不同的值簡洋提示:沒有order by時(shí),rownum按順序輸出,一旦有了order by,rownum不按順序輸出了,這說明rownum是排序前的編號(hào)。如果對(duì)order by從句中的字段建立了索引,那么,rownum也是按順序輸出的,因?yàn)檫@時(shí)候生成原始的查詢結(jié)果集時(shí)會(huì)參照索引表的順序來構(gòu)建。sqlserver:sql = select top 10 * from id not id(select top + (pageNo1)*pageSize + id from articles)DataSource ds = new InitialContext().lookup(jndiurl)。Connection = ()。//select * from user where id=? binary directivePreparedStatement pstmt = (sql)。ResultSet rs = ()while(()){ ((1))。}pageSize=20。pageNo = 5。 = nullstmt = null。rs = null。try{sqlserver:sql = select * from articles。DataSource ds = new InitialContext().lookup(jndiurl)。Connection = ()。//select * from user where id=? binary directivePreparedStatement pstmt = (sql)。ResultSet rs = ()for(int j=0。j(pageNo1)*pageSize。j++){ ()。}int i=0。while(() amp。amp。 i10){ i++。 ((1))。}}cacth(){}finnaly{ if(rs!=null)try{()。}catch(Exception e){} if(stm......... if(............}pageSize=20。pageNo = 5。 = nullstmt = null。rs = null。try{sqlserver:sql = select * from articles。DataSource ds = new InitialContext().lookup(jndiurl)。Connection = ()。//select * from user where id=? binary directivePreparedStatement pstmt = (sql,...)。//根據(jù)上面這行代碼的異常SQLFeatureNotSupportedException,就可判斷驅(qū)動(dòng)是否支持可滾動(dòng)游標(biāo)ResultSet rs = ()((pageNo1)*pageSize)int i=0。while(() amp。amp。 i10){ i++。 ((1))。}}cacth(){}finnaly{ if(rs!=null)try{()。}catch(Exception e){} if(stm......... if(............} 查詢出每門課都大于80分的學(xué)生姓名 name kecheng fenshu 張三 語文 81張三 數(shù)學(xué) 75李四 語文 76李四 數(shù)學(xué) 90王五 語文 81王五 數(shù)學(xué) 100王五 英語 90準(zhǔn)備數(shù)據(jù)的sql代碼:create table score(id int primary key auto_increment,name varchar(20),subject varchar(20),score int)。insert into score values (null,39。張三39。,39。語文39。,81),(null,39。張三39。,39。數(shù)學(xué)39。,75),(null,39。李四39。,39。語文39。,76),(null,39。李四39。,39。數(shù)學(xué)39。,90),(null,39。王五39。,39。語文39。,81),(null,39。王五39。,39。數(shù)學(xué)39。,100),(null,39。王五 39。,39。英語39。,90)。提示:當(dāng)百思不得其解時(shí),請(qǐng)理想思維,把小變成大做,把大變成小做,答案:A: select distinct name from score where name not in (select distinct name from score where score=80)B:select distince name t1 from score where 80 all (select score from score where name=t1)。一個(gè)叫department的表,里面只有一個(gè)字段name,一共有4條紀(jì)錄,分別是a,b,c,d,對(duì)應(yīng)四個(gè)球?qū)ΓF(xiàn)在四個(gè)球?qū)M(jìn)行比賽,用一條sql語句顯示所有可能的比賽組合.答:select , from team a, team b where 請(qǐng)用SQL語句實(shí)現(xiàn):從TestDB數(shù)據(jù)表中查詢出所有月份的發(fā)生額都比101科目相應(yīng)月份的發(fā)生額高的科目。請(qǐng)注意:TestDB中有很多科目,都有1-12月份的發(fā)生額。AccID:科目代碼,Occmonth:發(fā)生額月份,DebitOccur:發(fā)生額。數(shù)據(jù)庫名:JcyAudit,數(shù)據(jù)集:Select * from TestDB準(zhǔn)備數(shù)據(jù)的sql代碼:drop table if exists TestDB。create table TestDB(id int primary key auto_increment,AccID varchar(20), Occmonth date, DebitOccur bigint)。insert into TestDB values (null,39。10139。,39。19881139。,100),(null,39。10139。,39。19882139。,110),(null,39。10139。,39。19883139。,120),(null,39。10139。,39。19884139。,100),(null,39。10139。,39。19885139。,100),(null,39。10139。,39。19886139。,100),(null,39。10139。,39。19887139。,100),(null,39。10139。,39。19888139。,100)。復(fù)制上面的數(shù)據(jù),故意把第一個(gè)月份的發(fā)生額數(shù)字改小一點(diǎn)insert into TestDB values (null,39。10239。,39。19881139。,90),(null,39。10239。,39。19882139。,110),(null,39。10239。,39。19883139。,120),(null,39。10239。,39。19884139。,100),(null,39。10239。,39。19885139。,100),(null,39。10239。,39。19886139。,100),(null,39。10239。,39。19887139。,100),(null,39。10239。,39。19888139。,100)。復(fù)制最上面的數(shù)據(jù),故意把所有發(fā)生額數(shù)字改大一點(diǎn)insert into TestDB values (null,39。10339。,39。19881139。,150),(null,39。10339。,39。1988