1 -- 執(zhí)行插入語句返回剛剛生成的自動編號 2 insert into TblClass output inserted.ClsId values( ' 大一一班 ' , ' 11 ' , 18 ) 3 4 ------------CASE函數(shù)用法------------ 5 -- 相當(dāng)于switch 注意then后面的數(shù)據(jù)類型要一樣 6 select * from Tblscore 7 select 8 tScoreid, 9 tenglish, 10 評分= 11 case 12 when tenglish>= 95 then ' 優(yōu)秀 ' 13 when tenglish>= 90 then ' 良好 ' 14 when tenglish> 80 then ' 優(yōu) ' 15 when tenglish is null then ' 101 ' 16 else ' 喬布斯 ' 17 end 18 from TblScore 19 20 -- 等值判斷 21 select 22 tScoreid, 23 tenglish, 24 評分= 25 case tenglish 26 when 95 then ' 優(yōu)秀 ' 27 when 90 then ' 良好 ' 28 when 80 then ' 優(yōu) ' 29 when null then ' 101 ' 30 else ' 喬布斯 ' 31 end 32 from TblScore 33 34 35 36 -- 聚集索引(聚簇索引): 37 ------- 當(dāng)數(shù)據(jù)實(shí)際的存儲順序,與索引的順序一致就把該索引叫聚集索引 38 -- 非聚集索引(非聚簇索引) 39 ------- 當(dāng)索引中數(shù)據(jù)的順序與,數(shù)據(jù)實(shí)際存儲的順序不一致的時候,該索引叫非聚集索引。 40 =======非聚集索引============= 41 -- 在表Sales.SalesPerson中給SalesQuota, SalesYTD這兩列創(chuàng)建非聚集索引 42 CREATE NONCLUSTERED INDEX IX_SalesPerson_SalesQuota_SalesYTD ON Sales.SalesPerson (SalesQuota, SalesYTD); GO 43 ====創(chuàng)建唯一非聚集索引============= 44 CREATE UNIQUE INDEX AK_UnitMeasure_Name ON Production.UnitMeasure(Name); GO 45 =======創(chuàng)建聚集索引================= 46 CREATE TABLE t1 (a int , b int , c AS a/ b); 47 -- 創(chuàng)建唯一的聚集索引 48 CREATE UNIQUE CLUSTERED INDEX Idx1 ON t1(c); INSERT INTO t1 VALUES ( 1 , 0 ); 49 -- 刪除索引 50 drop index T8.IX_T8_tage 51 52 53 54 ----------------子查詢-------------------- 55 -- 把一個查詢結(jié)果作為另一個查詢的查詢源 56 57 select * from 58 ( select fname,fage,fgender from MyStudent 59 where fage between 18 and 24 and fgender= ' 女 ' ) as tbl 60 where fname like ' 趙% ' 61 62 -- 把另外一個查詢的結(jié)果作為當(dāng)前查詢的where條件來使用。 63 64 65 select * from tblstudent where tsclassid= 66 ( select tclassid from tblclass where tclassname= ' 高二二班 ' ) 67 68 ---exists----- 69 -- 如果exists包含了的查詢,能查到結(jié)果,就返回true,否則返回false 70 if (exists( select * from tblstudent where tsid<> 1 )) 71 begin 72 print ' 有數(shù)據(jù) ' 73 end 74 else 75 begin 76 print ' 無查詢結(jié)果 ' 77 end 78 79 80 --查詢所有 ' 高二二班 ' 與 ' 高二一班 ' 的學(xué)生的信息 81 --子查詢中=、!=、<、<=、>、>= 之后只能返回單個值,如果多個值就報錯了。 82 83 -- 這個寫法是錯誤的 84 select * from tblstudent where tsclassid= 85 ( 86 select tclassid from tblclass where tclassname= ' 高二二班 ' or tclassname= ' 高二一班 ' 87 ) 88 --- 這個寫法是正確的 89 select * from tblstudent where tsclassid in 90 ( 91 select tclassid from tblclass where tclassname= ' 高二二班 ' or tclassname= ' 高二一班 ' 92 ) 93 94 95 ------------------分頁---------------- 96 ------ desc 降序排序從高到底 asc升序排序(默認(rèn)) 97 98 -------第一種分頁---------------- 99 ----- 每頁5條數(shù)據(jù),找第二頁 100 select top 5 * from tblstudent where tsid not in 101 ( 102 select top(( 2 - 1 )* 5 ) tsid from tblstudent order by tsid 103 ) order by tsid 104 105 ---第二種分頁-- 106 select * from 107 ( 108 select *, row_number() over(order by tsid) as number from tblstudent 109 ) as T 110 where T.number between 6 and 10 111 112 113 114 -- 開窗函數(shù)與聚合函數(shù)一起使用,可以讓聚合函數(shù)對每一條數(shù)據(jù)都計算一次。 115 select * ,count(*) over() as ' 總條數(shù) ' from tblstudent 116 117 118 --------------連接join----------------- 119 -- 案例3:查詢學(xué)生姓名、年齡、班級及成績 120 select 121 ts.tsname as ' 學(xué)生姓名 ' , 122 ts.tsage as ' 年齡 ' , 123 tc.tclassname ' 班級 ' , 124 tb.tenglish ' 英語成績 ' , 125 tb.tmath ' 數(shù)學(xué)成績 ' 126 127 from tblstudent as ts 128 inner join tblscore as tb on ts.tsid= tb.tsid 129 inner join tblclass as tc on tc.tclassid= ts.tsclassid 130 131 132 -- 請查詢出所有沒有參加考試(在成績表中不存在的學(xué)生)的學(xué)生的姓名。 133 select ts.tsname 134 from tblstudent as ts 135 left join tblscore as tb on ts.tsid= tb.tsid 136 where tb.tenglish is null and tb.tmath is null
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
