




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第一章SQLcreateuser‘houjie410782’@’localhost’identifiedbygrantallprivilegeson*.*to‘BOOKSQL’@’LOCALHOST’withgrantoption;gratallprivilegeson*.*to‘houjie410782’@’localhost’withgrantoption;創(chuàng)建數(shù)據(jù)庫tennis。createdatabasetennis;usecreatetableyers(yernointegernotnull,namechar(15)notnull,initialschar(3)notnull,birth_datedate,char(1)notnull,joinedsmallintnotnull,housenochar(4),postcodephonenochar(13),leaguenochar(4),primarykey(yerno))createteams(teamnointegernotnull,yernointegernotnull,divisionchar(6)notnull,primarykey)createtablematches(matchnointegernotnull,teamnointegernotnull,yernointegernotnull,wonsmallintnotnull,lostsmallintnotnull,primarykey(matchno))createpenalties(paymentnointegernotnull,yernointegernotnull,payment_datedatenotnull,amountdecimal(7,2)notnull,primarykey(paymentno))createtablecommittee_members(yernointegernotnull,begin_datedatenotnull,end_datedate,positionprimarykey()insertintoyersvalues(2,'Everett','R','1948-09-01','M',1975,'Stoneyinsertintoyersvalues(7,'Wise','GWS','1963-05-11','M',1981,' insertintoyersvalues(8,'NewCastle','B','1962-07-08','F',1980,'Stationinsertintoyersvalues(27,'Collins','DD','1964-12-28','F',1983,'Longinsertintoyersvalues(28,'Collins','C','1963-06-22','F',1983,'OldMianinsertintoyersvalues(39,'Bishop','D','1956-10-29','M',1980,'Eatoninsertintoyersvalues(44,'Baker','E','1963-01-09','M',1980,'Lewisinsertintoyersvalues(57,'Brown','M','1971-08-17','M',1985,' insertintoyersvalues(83,'Hope','PK','1956-11-11','M',1982,'Magdaleneinsertintoyersvalues(95,'Miller','P','1963-05-14','M',1972,'Highinsertintoyersvalues(100,'Parmanter','P','1963-02-28','M',1979,'Haseltineinsertintoyersvalues(104,'Moorman','D','1970-05-10','F',1984,'Stoutinsertintoyersvalues(112,'Bailey','IP','1963-10-01','F',1984,'Vixeninsertintoyersvalues(6,'parmenter','R','1964-06-25','M',1977,'Haseltineinsertintoteamsvalues(1,6,'first');insertintomatchesvalues(1,1,6,3,1);insertintomatchesvalues(2,1,6,2,3);insertintomatchesvalues(3,1,6,3,0);insertintomatchesvalues(4,1,44,3,2);insertintomatchesvalues(5,1,83,0,3);insertintomatchesvalues(6,1,2,1,3);insertintomatchesvalues(7,1,2,1,3);insertintomatchesvalues(8,1,8,0,3);insertintomatchesvalues(9,2,27,3,2);insertintomatchesvalues(13,2,8,0,3);insertintopenaltiesvalues(1,6,'1980-12-08',100.00);insertintopenaltiesvalues(2,44,'1981-05-05',75.00);insertintopenaltiesvalues(3,27,'1983-09-10',100.00);insertintopenaltiesvalues(4,104,'1984-12-08',50.00);insertintopenaltiesvalues(5,44,'1980-12-08',25.00);insertintopenaltiesvalues(6,8,'1980-12-08',25.00);insertintopenaltiesvalues(7,44,'1982-12-30',30.00);insertintopenaltiesvalues(8,27,'1984-11-insertintocommittee_membersvalues(2,'1990-01-01','1992-12-31','Chairman');insertintocommittee_membersvalues(2,'1994-01-01',NULL,'Member');insertintocommittee_membersvalues(6,'1990-01-01','1990-12-31','Secretary');insertintocommittee_membersvalues(6,'1991-01-01','1992-12-31','Member');insertintocommittee_membersvalues(6,'1992-01-01','1993-12-31','Treasurer');insertintocommittee_membersvalues(6,'1993-01-01',NULL,'Chairman');insertintocommittee_membersvalues(8,'1990-01-01','1990-12-31','Treasurer');insertintocommittee_membersvalues(8,'1991-01-01','1991-12-31','Secretary');insertintocommittee_membersvalues(8,'1993-01-01','1993-12-31','Member');insertintocommittee_membersvalues(8,'1994-01-01',NULL,'Member');insertintocommittee_membersvalues(27,'1990-01-01','1990-12-31','Member');insertintocommittee_membersvalues(27,'1991-01-01','1991-12-31','Treasurer');insertintocommittee_membersvalues(27,'1993-01-01','1993-12-31','Treasurer');insertintocommittee_membersvalues(57,'1992-01-01','1992-12-31','Secretary');insertintocommittee_membersvalues(95,'1994-01-01',NULL,'Treasurer');insertintocommittee_membersvalues(112,'1992-01-01','1992-12-31','Member');insertintocommittee_membersvalues(112,'1994-01-01',NULL,'Secretary');selectfromorderbyselectfromorderbyyerno;select*fromselectselectyerno,amountfrompenaltieswhereupdatepenaltiessetamount=200whereyerno=44;fromwherecreateindexpenalties_amountoncreateviewnumber_sets(matchno,difference)asselecematchno,abs(win-lost)from第二章Select語句:常用元素createtableincorrect_date(column1insertintoincorrect_datevalues(‘2009-13-selectcolumn1fromfrommatcheswherewon-selectteamno,divisionfromteams;selectteamnoasteam_number,divisionasdivision_of_teamfromteams;selectpaymentno,amount*100ascentsfrompenalties;從matchesselectmatchnoasprimaryKey,80aswon-lostasdifference,time('23:59:59')as'Almost_midNight','Text'asTextfrommatcheswhereselectamount*100ascentsfrompenaltiesgroupbycentsorderbyselectyers.yerno,,yers.initialsfromyerswhereyers.set@,town,postcodefromyerswhereyerno<@select@SQL_WARNINGSsetset統(tǒng)變量sql_select_limit的方式為:selectselectcurrent_date;selectcurrent_time;selectcurrent_user;selectyerno,name,when'F'then'Female'when'M'then'Male'endfromyersselectyerno,town,birth_date,casetownwhen'Stratford'then'ST'when'Plymouth'then'PM'when'Inglewood'then'IW'else'OT'casetowncasebirth_datewhen'1948-09-01'then'OldStratford'else'YoungStratford'casebirth_datewhen'1962-07-08'then'OldIngelwood'else'YoungInglewood'endastypefromyers;selectyerno,joined,whenjoined<1980thenwhenjoined<1983then‘jubior’else‘children’fromyersorderbyjoined;selectyerno,joined,town,whenjoined>=1980andjoined<1983then‘senior’whentown=’Eltham’then‘Elthammers’whenyerno<10then‘first_members’else‘Rest’endaslevelfromyers;selectpaymentno,year(payment_date)frompenaltieswherefromwhere空,將其顯示為1。fromyerswhereselectyerno,dayname(birth_date),monthname(birth_date),dayofyear(birth_date)fromyerswhereselectyerno,birth_date,adddate(birth_date,interval7day)asadd7daysfromyerswheredayname(birth_date)='Saturday';fromcommittee_membersor(end_dateisnullorderbyyerno;selectyerno,begin_date,end_date,fromcommittee_membersorderbyyerno;selectpaymentnofrompenaltiesselectconcat(rtrim(name),cast(birth_dateaschar(10)))fromyerswhereupdatewhereyerno=2;selectteamno,cast(NULLaschar)fromteams;selectmatchno,won,lostfrommatcheswhere把502selectselectB’11’<<3;selectconv(6,10,2),conv(10,10,2),bin(6),bin(10);33.獲取屬于二進制表示1001和111的十進制值。selectconv(1001,2,10),conv(111,2,10);selectfromyerswhereyerno&1;selectyerno,namefromyerswhereyerno=(frommatches;selectyerno,town||’’||street||’’||housenoasAddressfromfromwhereselectpaymentno,payment_date,payment_date+interval7dayfrompenaltieswhereselectpaymentno,payment_date,adddate(payment_date,interval7day)asadd7daysfrompenaltieswhereselectpaymentno,payment_datefrompenalties25’,interval7selectadddate(‘2004-13-12’,interval1day);showcreatetablematches_special(matchnointegernotnull,teamnointegernotnull,yernointegernotnull,wonsmallintnotnull,lostsmallintnotnull,start_datedatenotnull,start_timetimenotnull,end_datedatenotnull,end_timetimenotnull,primarykey(matchno))selectmatchno,start_time,addtime(start_time,’08:00:00’)frommatch_special;selectfromwhereaddtime(end_time,’06:30:30’)<=’24:00:00’;createtabletstamp(col1timestamp);set@time=timestamp(‘1980-12-0823:59:59.59’);insertintotstampvalues(@time+interval3microsecond);selectcol1,col1+interval3microsecondfromtstamp;selectteamnofromteamswheretrueor等同于:selectteamnofromteams;因為trueorfalse為真指明那些罰款的編號大于4。frompenalties;selectyerno,joined,yerno<15,joined>1979fromyersStratfordHaseltineLaneselectfrom的selectyerno,namefromyers答案通49題。第三 SELECT語句、表表達式和子查selectfromyers,penaltieswhereamount>25havingcount(*)>1orderbyyerno;selectyerno,leaguenofromyersorderbyselectyernofromteamsselectyernofromselectyernofrom(selectyerno,fromwhereyerno<10)asyer10 selectyernofrom(selectyerno,from(selectyerno, fromwhereyerno>10)asgreater10whereyerno<100)aslessthan100 selectfromwhereyerno<100andyerno>10andjoined>1980 selectfromwhereyerno=100)asfromwhereselectfromwhereyear(birth_date)=(selectfromwhere(selectbirth_datefromyerswhereyerno=27),(selectbirth_datefromyerswhereyerno=44),(selectbirth_datefromyerswhereselectfromwhere fromwhere第四章SELECT子句:FROM子句createdatabaseextra;useextra;createtablecities(citynointegernotcitynamechar(20)notnull,primarykey(cityno));insertintocitiesselect*fromselectteamno,namefromteams,selectyers.yerno,,yers.initials,fromfromyersasp,penaltiesaspenselectt.fromteamsast,penaltiesaspenwheret.yerno=pen.selectdistinctt.fromteamsast,penaltiesaspenwheret.yerno=,p.initialsfromyersasp,matchesasmwherep.yerno=m.yerno;selectm.matchno,p.yerno,m.teamno,,t.divisionfrommatchesasm,yersasp,teamsastwherep.yerno=m.yernoandfromyersasp,penaltiesaspenwherepen.yerno=p.yernoandselectp.fromyersasp,='Parmenter'andpar.initials='R'andp.birth_date<par.birth_date;selectp.yernofromyersasp,extra.citiesselectp.yerno,name,pen.amountfromyersasp,penaltiesaspenselectp.fromyersaspinnerjoinpenaltiesaspenon(p.yerno=pen.yerno)wherep.birth_date>'1920-06-30';selectt.teamno,fromteamsast,yersaspwheret.yerno=p.selectfromteamsastinnerjoinyersaspon(t.yerno=p.yerno);15.(左外連接)對于所有球員,獲得球員號碼,名字和他所引起的罰款,結果按照球員號碼排selectp.yerno,name,pen.amountfromyersasp,penaltiesaspenwherep.yerno=pen.yernoorderbyp.yerno;但是這樣的寫法是不完整的,為了強調所有球員selectp.fromyersaspleftouterjoinpenaltiesaspenonp.yerno=pen.yernoorderbyp.左外連接是否必要,取決于問題和連接之間的關系,在p.yerno和pen.yerno之間,存在selectfrompenaltiesaspenleftouterjoinyersasponpen.yerno=p.yernoorderby對于每個球員selectp.yerno,name,t.teamno,t.divisionfromyersaspleftouterjointeamsastonp.yerno=t.yernoorderbyp.Inglewood每個球員,獲取球員編號,名字,罰款列表并且列出他曾經(jīng)效力過selectp.yerno,,pen.amount,m.teamnofromyersaspleftouterjoinpenaltiesaspenonp.yerno=pen.yernoleftouterjoinmatchesasmonp.yerno=m.yernowhere19.(右外連接)對于所有球員selectp.fromteamsastrightouterjoinyersaspont.yerno=p.yerno;selectp.yerno,,pen.amountfromyersasp,penaltiesaspenselectp.fromyersaspnaturaljoinpenaltiesaspenwherep.birth_date>'1920-06-30';fromteamsleftouterjoinpenaltieswhere如:fromteamsinnerjoinonteams.yerno=yers.fromteamsinnerjoinyersonusing(yerno)select*frompenaltiesleftjoinusing(selectp.yernofromyersaspwhereselectfrom(select*fromwheretown='Stratford')asselectsmall_teams.yernofrom(selectyerno,divisionfromteams)assmall_teamsselectfrommatches)asmwhere創(chuàng)建一個名為townsselectfrom(select'Stratford'astown,4asselect'Douglas',2)astownsorderbytown;fromyersleftouterjoin(select'Stratford'astown,4asnumberselect'Douglas',2)astownsorderbyyerno;selectfromyersleftouter(select'Stratford'astown,4asselect'Douglas',2)astownsonyers.town=towns.townwheretowns.number>2;selectselect'Arnold')asfirst_names,(select'Berg'aslast_nameselect'William')asselectnumber,power(number,3)from(select10asselect19)asnumbersselectnumberfrom(selectcast(concat(digit1.digit,concat(digit2.digit,digit3.digit))asunsignedinteger)asnumberfrom(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’unionselect‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselect‘8’unionselect‘9’)asdigit1,(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’unionselect‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselect‘8’unionselect‘9’)asdigit2,(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’unionselect‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselect‘8’unionselect‘9’)asdigit3)asnumbersorderbynumber;selectnumberassquare,round(sqrt(number))asfrom(selectcast(concat(digit1.digit,concat(digit2.digit,digit3.digit))asunsignedinteger)asnumberfrom(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’unionselect‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselect‘8’unionselect‘9’)asdigit1,(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’unionselect‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselect‘8’unionselect‘9’)asdigit2,(select‘0’asdigitunionselect‘1’unionselect‘2’unionselect‘3’select‘4’unionselect‘5’unionselect‘6’unionselect‘7’unionselectunionselect‘9’)asdigit3)asnumbersorderbynumber;第五章SELECT語句:WHERE子selectfromwhereselectyerno,birth_date,joinedfromyerswhereselectfromwhereselectyerno,leaguenofromyerswhereselectyerno,leaguenofromyerswhere者是非空值但不相等的時候,結果為false,這個條件的結果不會是unknown。selectmatchnofrommatcheswherewon=2andfrommatcheswhere(2,4)>(1,3)并不等價于(2>1)and(4>3),而是等價于:(2>1)or(2=1and (E1?E4)or(E1=E4andE2?E5)or(E1=E4andE2=E5andselectyers.yerno,fromyers,teamswhereyers.yerno=teams.yernoandselectyerno,namefromyerswhere(selectyernofromteamswhereselectyerno,name,initialsfromyerswhere(selectbirth_datefromyerswhereselectmatchnofrommatches(selectteamnofromteamswhereselectfromwhereleagueno<=>(selectleaguenofromyerswhereselectyerno,town,fromyers(selecttownfromyerswhereyerno=7), fromyerswhereyerno=2));selectdistinctyernofromcommittee_memberswherewhereyerno=6andselectyerno,name,initialsfromyerswhere(name,initials)<(selectname,initialsfromyerswhereyerno=6);selectmatchnowhere(start_date,start_time)>(selectstart_date,start_timefrommatch_specialwhereselectfrommatches,yerswhereyers.yerno=matches.yernoandselectmatchno,yerno,teamnofrommatcheswhereyerno=(selectyernofromteamswhereteams.yerno=matches.selectmatchnofromwheresubstr((selectdivisionfromteamswhere=substr((selectnamefromyerswhereyers.yerno=matches.selectyerno,leaguenofromyerswhereselectteamnofromteamswhereselectyerno,name, fromyerswhereyear(birth_date)>'1970-12-31' selectyerno,townfromyerswheretown='Plymouth'orselectyerno,name,townfromyerswhere或者:…wherenotselectyerno,leagueno,phonenofromyerswhereleaguenoand含那些居住在Stratford并且出生于1963年的球員。selectyerno,town,birth_datefromyerswhere(town='Stratford'oryear(birth_date)='1963')andnot(year(birth_date)='1963'andtown='Stratford');25inInglewoodPlymouthMidhurstDouglasselectfromwheretown='Inglewood'ortown='Plymouth'ortown='Midhurst'orselectyerno,name,townfromyers selectyerno,name,birth_datefromyerswhereyear(birth_date)inselectmatchno,won,lostfrommatcheswhere2inselectfromwhereyernoin(selectyernofrompenaltieswherepaymentno=1),(selectyernofromteamswhereteamno=2));selectmatchno,won,lostfromwherewonin(truncate(matchno/(selectlostfrommatcheswhereselectmatchnofromwhere(selectsubstr(name,1,1)fromwhereyers.yerno=matches.yerno)inselectmatchno,won,lostfrommatcheswherewon/lostin或者…wherewon,lost)inselectyerno,name,initialsfromyerswhere(name,initials)in(selectname,initialsfromyerswhereyerno=6),(selectname,initialsfromyerswhereyerno=27));selectyerno,name,initialsfromyerswhereyernoin(selectyernofromselectyerno,namefromyerswhereyernoin(selectyernofrommatcheswhereselectyerno,namefromyerswhereyerno(selectyernofrommatcheswhereteamnonotin(selectteamnofromteamswhereyerno=6));where(begin_date,end_date)in(selectbegin_date,end_datefromcommittee_memberswhereposition='Secretary');selectname,initials,townfromyerswhereyernoin(selectyernofromselectyerno,name,birth_datefromyerswhereyear(birth_date)between'1962and1964';392,34frommatcheswherewon+lostbetween2andselectyerno,name,birth_date,initialsfromyerswherebirth_datebetween(selectbirth_datefromyerswherename='Newcastle'and(selectbirth_datefromyerswherename='Miller'andinitials='P');41.(like運算符)找出名字以大寫字母B開頭的每個球員的名字和號碼。selectname,yernofromwherenamelike…whereselectname,yernofromyerswherenamelikeselectname,fromwherenamelikeselectname,town,yernofromyerswherenamelikeselectname,yernofromyerswherenamelike'%#_%'escapeselectname,yernofromyerswherenameregexpselectname,yernofromyerswherenameregexp'^ba';fromyerswherenameregexpselectname,yernofromwherenameregexpselectname,yernofromyerswherenameregexp'm.n';selectname,yernofromyerswherenameregexpselectyerno,postcodefromyerswherepostcoderegexp'^[0-9][0-9]3';或…wherepostcodelike'3%';selectstreet,yernofromyerswherestreetregexpselectyerno,postcodefromyerswherepostcoderegexp'[0-9]+[a-z]+';到selectname,yernofromyerswherenameregexp'^[^A-M]';或selectname,yernofromwheresubstr(name,1,1)notinselectyerno,namefromyerswherenameregexp'^[a-z]{7}';selectyerno,namefromyerswherenameregexp'^[a-z]{6,7}$';selectyerno,postcodefromyerswherepostcoderegexp'4{4}';selectyerno,streetfromyersselectyerno,namefromyerswherenameregexp'[[.space.]]';selectyerno,streetfromyerswherestreetregexpStreet[[:>:]]';selectyerno,streetfromyerswherestreetlike'%street%';createtable(booknointegernotnullprimarykey,authorstextnotnull,titletextnotnull,year_publicationyearnotnull,summarytextnotnull);給booksselectyerno,leaguenofromyerswhereleaguenoisnotnullselectname,yerno,leaguenofromyerswhereleagueno<>'8467'orleaguenoisnull;selectname,initialsfromwhereyernoin(selectyernofromselectname,initialsfromyerswhereexists(select*frompenaltieswhereyerno=yers.selectname,initialsfromyerswherenot(select*fromteamswhereyerno=yers.selectyerno,name,birth_datefromyerswherebirth_date<=all(selectbirth_datefromselectyerno,birth_datefromyerswhere(selectbirth_datefromyersaspinnerjoinmatchesasmonp.yerno=m.yernowherem.teamno=2);selectteamno,yernofrommatchesasm1where(selectwonfrommatchesasm2whereselectleagueno,yernofromyerswhereleagueno>=all(selectleaguenofromyers);selectyerno,town,leaguenofromyersasp1whereleagueno<=all(selectp2.leaguenofromyersasp2wherep1.town=p2.townandleaguenoisnotnull);selectyerno,name,birth_datefromyerswhere(selectbirth_datefromselectdistinctyernofrompenaltieswhereyerno<>27(selectamountfrompenaltieswhereselectyerno,birth_date,townfromyersasp1where(selectbirth_datefromyersasp2whereselectyerno,namefromyerswhereyerno(selectyernofromselectyerno,namefromyersaspwhereexists(select*frompenaltiesaspenselectteamno,divisionfromteamswhereexists(select*frommatcheswhereyerno=44and1selectdistinctyernofrompenaltiesaspenwhereyernoin(selectyernofrompenaltieswhere獲取那些沒有為1selectyerno,namefromyerswhere1<>all(selectteamnofrommatcheswhereyerno=yers.selectteamnofromteamswherenotexists(select*frommatcheswhereyerno=27andselectyernofromyersaspwherenotexists(select*fromteamsastwherenotexists(select*frommatchesaswheret.teamno=m.teamnoandp.yerno=m.selectyernofromyerswherenotexists(select*frommatchesasm1whereyerno=57andnotexists(select*frommatchesasm2wherem1.teamno=m2.teamnoandselectyernofrommatcheswhereteamnoin(selectteamnofromteamswhereteamnonotin(selectteamnofrommatcheswherematchno=57));selectyernofromyerswheretown='Stratford';獲取那些引起一次25罰款的每個球員號碼selectyernofrompenaltieswhere第六章select語句:select子句和聚合函數(shù)獲取整個penaltiesselect*fromselectpenalties.*frompenaltiesinnerjointeamsonpenalties.yerno=teams.yerno;selectpenalties.*frompenalties,teamsselectdistincttownfromselectdistinctstreet,townfromselectdistinctleaguenofromselectcount(*)asnumbersfromselectcount(*)fromyerswhereselectcount(allleagueno)fromyers;10town列中,有多少不同的城市名?selectcount(distincttown)fromyers;selectcount(distinctsubstr(name,1,1))fromselectcount(distinctyear(payment_date))fromselectcount(distinct )fromselectyerno,namefromyersaspwhere(selectcount(*)frompenaltiesaspenwherep.yerno=pen.>(selectcount(*)frommatchesasmwherep.yerno=m.select(selectcount(*)frompenaltieswherepenalties.yerno=yers.yerno)asnumbersfromyerswhere(selectcount(*)frompenaltieswherepenalties.yerno=yers.(selectcount(*)frompenalties),(selectcount(*)fromselectmax(amount)fromselectmin(amount)frompenaltieswhereyernoin(selectyernofromyerswherewheretown='Stratford'andyers.yerno=penalties.selectcount(*)frompenaltieswhereselectmin(amount)fromselect(max(amount)-min(amount))*100asdiff_centsfromselectsubstr(max(name),1,1)fromyersorderbyselectmax(leagueno)fromyerswheretown='Midhurst';輸出文本Unknown。selectcasewhenmin(leagueno)isnullthen'Unknown'elseminfromyerswhereselectyerno,amount,payment_datefrompenaltiesaspen1whereamount=(selectmax(amount)frompenaltiesasselect(selectmax(amount)frompenaltieswherepenalties.yerno=yers.yerno)as(selectmax(won)frommatcheswherematches.yerno=yers.yerno)asfromselectfrom=selectsum(amount)fromwhereyernoin(selectyernofromyerswhereselectavg(amount)frompenaltieswhereselectdistinctyernofrompenaltieswhereamount>(selectavg(amount)fromselectavg(distinctamountfrompenalties;selectavg(length(rtrim(name))),max(length(rtrim(name)))fromyers;selectpaymentno,amount,abs(amount-(selectavg(amountfrompenaltiesasfromselectvariance(amount)frompenaltieswhereselectstddev(amount)frompenaltieswhereselectvar_samp(amount),variance(amount)fromselectstddev_samp(amount),stddev(amount)from第七章SELECTGROUPBY子句selecttownfromyersgroupbytown;selecttown,count(*)fromyersgroupbytown;selectteamno,count(*),sum(won)frommatchesgroupbyselectteamno,count(*)frommatcheswhereteamno(selectteamnofromteamsinnerjoinyersonteams.yerno=yers.yernowheretown='Eltham')groupbyselectamount,count(*),amount*count(*)assum_amountfrompenaltiesgroupbyselectteamno,yernofrommatchesgroupbyteamno,yerno;selectp.fromyersaspinnerjoinpenaltiesaspenonp.yerno=pen.yernogroupbyp.selectyear(payment_date),count(*)frompenaltiesgroupbyselecttruncate(yerno/25,0),count(*),max(yerno)fromyersgroupbytruncate(selectyerno,name,leaguenofromyersgroupbyleagueno;selectmatchno,teamno,count(*)frommatchesgroupbyteamnoorderbyteamnodesc1groupbyselectgroupby現(xiàn)在groupby子句中的表達式也可以出現(xiàn)在select子句中。groupby子句groupbytown,towngroupbytown13.(group_concat函數(shù))對于每個球隊,獲取球隊的號碼和為該七對效力的球員列表。selectteamno,group_concat(yerno)frommatchesselectteamno,group_concat(teamno)frommatchesgroupbyselectgroup_concat(paymentno)frompenalties;selectfrom(selectyerno,sum(amount)astotalfrompenaltiesgroupbyyerno)astotalswhereyernoin(selectyernofromyerswheretown='Stratford'orselectyers.fromyers,(selectyerno,count(*)asnumber_of_penaltiesfrompenaltiesgroupbyyerno)asnumber_penalties,(selectyerno,count(*)asnumber_of_teamsfromteamsgroupbyyerno)aswhereyers.yerno=number_penalties.yernoandselectdistinctm.frommatchesasmleftouterjoin(selectyerno,count(*)asnumberpfrompenaltiesgroupbyyerno)asnp 第二組包含1981年7月1日到1982年12月31日之間的所有罰款,對于每一組,得到罰款的frompenaltiesasp,(select1aspgroup,'1980-01-01'asstart,'1981-06-30'asendselect3,'1983-01-01','1984-12-31')asgroupswherep.payment_datebetweenstartandendgroupbygroups.pgrouporderbyfrompenaltiesasp1,penaltiesasp2groupbyp1.paymentno,p1.amountorderbyp1.paymentno;selectp1.paymentno,p1.amount,(p1.amount*100)/sum(p2.amount)frompenaltiesasp1,penaltiesasp2orderbyp1.paymentno;selectyerno,sum(amount)frompenaltiesgroupbyyernoselectnull,sum(amount)fromselectyerno,sum(amount)frompenaltiesgroupbyyernowith fromyersgroup ,townwith第八章select語句:having子句selectyernofrompenaltiesgroupbyyernohavingcount(*)>selectyernofrompenaltiesgroupbyyernohavingselectyerno,sum(amount)frompenaltiesgroupbyhavingselectyerno,sum(amount)frompenaltieswhereyernoin(selectyernofromteams)groupbyyernohavingselectyerno,sum(amount)frompenaltiesgroupbyhaving(selectsum(amount)frompenaltiesgroupbyselectsum(amount)frompenaltieshaving第九章select語句:orderby子句selectpaymentno,yernofrompenaltiesorderbyselectyerno,amountfrompenaltiesorderbyselectamountorderbyselectname,initials,yernofromyersorderbysubstr(name,1,1);selectyerno,amountfromorderbyabs(amount-(selectavg(amount)fromselectyerno,amountfrompenaltiesasorderby(selectavg(amount)frompenaltiesasp2wherep1.yerno=p2.yerno);selectyerno,sum(amount)frompenaltiesgroupbyyernoorderbyselectyerno,name,(selectsum(amount)frompenaltiesaspenwherepen.yerno=p.yerno)fromyersasporderby3;selectyerno,amountfrompenaltiesorderbyyernodesc,amountasc;selectdistinctleaguenofromyersorderbyleaguenodesc;第十章select語句:LIMIT子句selectyerno,namefromyersorderbyyernodesclimitselectleagueno,yerno,namefromyersorderbyleaguenoasclimitselectyerno,count(*)asnumbersfrommatcheswherewon>lostgroupbyyernoorderbyyernodesclimit3;selectyerno,count(*)asnumbersfrommatcheswherewon>lostgroupbyorderbynumbersdesc,yernodesclimit3;selectfrom(selectyerno,count(*)asnumbersfrommatcheswherewon>lostgroupbyorderbynumbersdesc,yernodesclimit3)asT;selectavg(amount)(selectamountfrompenaltiesorderbyamountlimit4)as第3高的罰款額是多少?orderbyamountdesclimit3)asselectdistinctamountfrompenaltiesorderbyamountdesclimitselectyerno(selectyernofromyerswhereleaguenoisnotnullorderbyleaguenodesclimit6)asTorderbyyernoasclimitselectyerno,namefromyerswhereyernoin(selectyerno(selectyerno,sum(amount)astotalfrompenaltiesgroupbyyernoorderbytotaldesclimit3)asT);11.(出不來正確結果)獲取那些至少引起了一次罰款,并且罰款額不等于最高的兩個之一,也selectyerno,namefromyerswhereyernoin(selectyenrofrompenalties)yernonot(selectyernofrompenaltiesorderbyamountdesclimit2)andyernonotin(selectyernofrompenaltiesorderbyamountasclimitselectyerno,namefromyersorderbyyernoasclimit5offset3selectsql_calc_found_rowspaymentnofrompenaltieslimit第十一章組合表達式selectyerno,townfromyerswheretown='Inglewood'selectyerno,townfromyerswheretown='Plymouth';selectbirth_dateasdatefromyersselectpayment_dateasdatefromselectyernofrompenaltiesselectyernofromselectyernofrompenaltiesselectyernofromteamsselectyernofromyerswheretown=selectcast(teamnoaschar(4))asTEAMNO,cast(yernoaschar(4))asYERNO,sum(won+lost)astotalfromfrommatchesfrommatchesorderbyselectyernofrompenaltiesunionallselectyernofrom第十二章用戶變量和SET set select@PIasselectname,town,postcodefromyerswhereset@abc=5,@def='Inglewood',@ghi=date('2001-09-用一個來定義用戶變量PI。set@PI=cast(22asBINARY)/7.0;set@ANR=(selectyernofromteamswhereteamno=select@yerno:=name,townpostcodeselectselect@name:=name,@town:=town,@postcode:=postcodefromyerswhereyerno=select@penaltiestotal:=sum(amount),@numberpenalties:=count(*)frompenalties;select@yerno:=yernofromyersorderbyyernodesc;select@yerno;set@CNO=(selectyernofromteamswhereteamno=1);selectnamefromyerswhereyerno=@CNO;select*fromwherepaymentno<@varandcreatetablevaribles(varnamechar(30)notnullprimarykey,varvaluechar(30)notnull);set@var1=100,@var2=insertintovariblesvaluesvar1',@var1);insertintovariblesvaluesvar2',@var2);select@var1:=varvaluefromvaribleswhereselect@var2:=varvaluefromvaribleswhereselect@var1,docurrent_date+interval2第十四章HANDLER語句penaltieshandlerpenaltiesreadfirst;//penalties表的第一handlerpenaltiescreateindexpenalties_amountonhandlerpenaltiesopenashandlerPreadpenalties_amountfirst;handlerPreadpenalties_amountnext;handlerPreadpenalties-amountlast;handlerPreadpenalties_amountprev;handlerpenalti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 除塵設備產業(yè)分析報告
- 抗血吸蟲病藥戰(zhàn)略市場規(guī)劃報告
- 對頂角、余角和補角 教案 2024-2025學年北師大版數(shù)學七年級下冊
- 廠房使用合同范本
- 受托支付合同范本簡易
- 化肥提供合同范本
- 機械基礎考試模擬題+參考答案
- 信息保密合同范本
- 賣房給中介合同范本
- 保姆合同范本帶小孩
- 園林綠化養(yǎng)護標準及經(jīng)費測算
- 結構力學本構模型:粘彈性模型:粘彈性模型的數(shù)值模擬技術
- 2024年山東高考政治試卷
- SF-36生活質量調查表(SF-36-含評分細則)
- DL-T5845-2021輸電線路巖石地基挖孔基礎工程技術規(guī)范
- 小故事大道理兩只山羊
- GB 19522-2024車輛駕駛人員血液、呼氣酒精含量閾值與檢驗
- 水泥窯替代燃料技術改造項目可行性研究報告
- 婦女兩癌篩查培訓
- 印刷品承印五項管理新規(guī)制度
- 2024年湖南鐵路科技職業(yè)技術學院單招職業(yè)技能測試題庫及答案解析
評論
0/150
提交評論