1 -- 聚合函數MAX(最大值)、MIN(最小值)、AVG (平均值)、SUM (和)、COUNT(數量:記錄的條數。) 2 3 -- 查詢數學成績中最高分是多少分 4 select max (fMath) as 數學成績最高分 from MyStudent 5 6 -- 求總分 7 select sum (fMath) as 總分 from MyStudent 8 9 -- 求平均分(對空值不處理) 10 select avg (fMath) as 平均分 from MyStudent 11 12 -- 求班級中總的記錄條數(總人數)也不考慮空值 13 select count ( * ) as 總人數 from MyStudent 14 15 16 -- ---------多條件查詢---------- 17 18 -- 查詢年齡在20--30歲之間的男學生 19 select 20 * 21 from MyStudent 22 where age between 18 and 30 and gender = ' 男 ' 23 -- -between 18 and 30 也相當于是>=18 and <=30 24 25 -- 查詢班級id為1 2 3的所有學生 26 select * from Student 27 where ClassId = 1 or ClassId = 2 or ClassId = 3 28 -- 可以用in()替代多個or 29 select * from Student 30 where ClassId in ( 1 , 2 , 3 ) 31 32 -- ----------模糊查詢------------ 33 34 -- 查詢出所有姓‘趙’的同學 35 -- 通配符%表示;任意多個任意字符 36 select * from MyStudent where name like ' 趙% ' 37 38 -- 查詢出姓名中包含‘敏’字的同學 39 select * from MyStudent where name like ' %敏% ' 40 41 -- 查詢出所有姓‘趙’的同學,并且姓名是三個字的 42 -- 通配符_ 表示任意的單個字符 43 select * from MyStudent where name like ' 趙__ ' 44 45 -- 查詢出姓名中包含‘趙’或‘云’的人的姓名 46 -- 通配符[]表示括號中的任一個字符 只選一個匹配 47 select * from MyStudent where name like ' [趙云] ' 48 49 -- 表示X與Y之間只要不是'趙'或‘云’的任意單個字 50 -- [^]表示除了中括號中的任意單個字符 51 select * from MyStudent where name like ' [^趙云] ' 52 53 -- 查詢姓名中包含%的 54 -- 用[]括起來,就表示轉義 55 select * from MyStudent where name like ' [%] ' 56 57 -- 查詢出所有不姓趙的同學 58 select * from MyStudent where name not like ' 趙% ' 59 60 -- 查詢出學生成績中數學成績為空的人 61 -- null在數據庫中表示unkonw(不知道),判斷一個值是否為null,也就不能用=或者<>來判斷 62 select * from MyStudet where Math = null 63 64 -- null與null比較結果還是null(null就表示不知道,"不知道"在where中就認為是false,所以不返回任何數據) 65 select * from MyStudent where Math <> null 66 67 -- 查詢所有math為非null的值 68 select * from MyStudent where Math id not null 69 70 71 select * from tblstudent group by TSGENDER 72 -- 請從學生表中查詢出每個班的班級Id和班級中男同學的人數: 73 select 74 tsclassid as 班級id, 75 count ( * ) as 班級人數 76 from TblStudent 77 where tsgender = ' 男 ' 78 group by tsclassid 79 -- 一般分組語句group by 都要與聚合函數配合使用,如果沒有聚合函數,分組的意義不大。 80 81 -- 當在select查詢語句中出現聚合函數時,這時不能在select查詢中再出現其他列,除非:該列也在group子句中出現或者也在聚合函數中出現。 82 83 84 85 -- 請從學生表中查詢出每個班的班級Id和班級中男同學的人數并且班級人數大于2: 86 select 87 tsclassid as 班級id, 88 count ( * ) as 班級人數 89 from TblStudent 90 where tsgender = ' 男 ' 91 group by tsclassid 92 having count ( * ) > 2 93 94 -- having語句后能跟什么列,主要看:分組后得到的結果集中包含什么列。 95 96 -- -------執行的順序------ 97 select 98 -- distinct / top 之類的關鍵字(這些都是一些現實的選項) 99 fgender as 性別, -- 5>選擇列 100 count ( * ) as 人數 101 from MyStudent -- 1>先從MyStudent表中拿到數據(全部數據的一個結果集) 102 where fage > 30 -- 2>從MyStudent的數據中篩選出所有年齡大于30歲的人的信息(新結果集,都是年齡大于30的) 103 group by fgender -- 3>按照性別分組,分完組以后又得到一個新結果集(分組后的結果) 104 having count ( * ) > 355 -- 4>基于分組以后的結果集,然后在篩選,篩選出那些組中記錄大于500的組。 105 order by 人數 desc -- 6>最后把顯示出來的結果排序 106 107 108 -- ------------類型轉換--------------- 109 -- select 100+'hello' 110 select cast ( 100 as varchar ( 10 )) + ' hello ' 111 112 select convert ( varchar ( 10 ), 100 ) + ' hello ' 113 114 select convert ( varchar ( 50 ), getdate ()) 115 select convert ( varchar ( 50 ), getdate (), 101 ) 116 select convert ( varchar ( 50 ), getdate (), 100 ) 117 select convert ( varchar ( 50 ), getdate (), 111 ) 118 select convert ( varchar ( 10 ), getdate (), 126 ) 119 120 121 122 -- --------------聯合---------------------- 123 -- union 的作用就是將多個結果集并到了一起 124 select tsName,tsAge from tblstudent 125 union all 126 select TTName,tTAge from DATA.dbo.TblTeacher 127 128 -- 聯合要注意的地方 129 -- 1.多個結果集中的列的數據類型必須一一對應 130 -- 2.列的個數必須一樣 131 -- 聯合的時候如果只寫union 則會去除重復數據,如果寫unoin all 則不會去除重復的數據 132 133 134 -- 假設有個銷售表,請查詢出每個銷售員的銷售總金額以及總的銷售金額 135 -- 要求:總的銷售金額顯示在表的底部 136 select 137 銷售員, 138 銷售金額 = sum (銷售數量 * 銷售價格) 139 from MyOrders 140 group by 銷售員 141 union all 142 select ' 總銷售額: ' , sum (銷售價格 * 銷售數量) 143 from MyOrders 144 145 146 147 -- ---------字符串函數---------------- 148 select len ( ' 長度abc ' ) -- 返回字符的個數 5 149 select datalength ( ' 長度abc ' ) -- 返回是字節的個數 7 150 select lower ( ' Abc ' ) -- 返回abc 151 select upper ( ' Abc ' ) -- 返回ABC 152 153 -- LTRIM():字符串左側的空格去掉 154 -- RTRIM () :字符串右側的空格去掉 155 156 select ' =========== ' + rtrim ( ltrim ( ' aaa ' )) + ' =============== ' 157 -- 返回===========aaa=============== 158 159 -- LEFT()、RIGHT() 截取取字符串 160 select left ( ' abcdefg ' , 2 ) -- 返回ab 161 select right ( ' abcdefg ' , 2 ) -- 返回fg 162 163 -- SUBSTRING(string,start_position,length),索引從1開始。 164 select substring ( ' abcdefg ' , 2 , 3 ) -- 返回bcd 165 166 167 168 -- -------- 日期函數------------ 169 -- getdate 取得當前日期時間 170 select getdate () -- 2012-08-23 18:09:00.540 171 -- DATEADD (datepart , number, date ),計算增加以后的日期。參數date為待計算的日期;參數number為增量 172 -- 參數datepart為計量單位 173 174 select dateadd ( month , 2 , getdate ()) -- 2012-10-23 18:09:13.527 175 176 -- DATEDIFF ( datepart , startdate , enddate ) :計算兩個日期之間的差額。 datepart 為計量單位 177 select datediff (second, ' 2012-08-23 18:09:00.540 ' , ' 2012-8-23 18:09:17.527 ' ) -- 17 minute 分 178 179 -- DATEPART (datepart,date):返回一個日期的特定部分 180 -- Month()、year()、day()來代替 181 select year ( getdate ()) -- 2012
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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