码迷,mamicode.com
首页 > 数据库 > 详细

MSSQL DBOtherSQL

时间:2016-03-07 22:38:23      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:

  1 --------------------------查询表中的数据------------------------------
  2 
  3 --1.请查询出MyStudent表中的所有数据
  4 --下面的语句表示查询出MyStudent表中的所有行、所有列
  5 select * from MyStudent
  6 
  7 --2.只显示部分列的数据(现实所有行,但是只查看指定的列)
  8 select fname,fage,fgender from mystudent
  9 
 10 --3.请查询出年龄在20-24岁之间的所有女同学的姓名,年龄,性别,学号
 11 select fname,fage,fgender,fid from MyStudent
 12 where fgender= and fage>=20 and fage<=24
 13 
 14 --4.为列起别名
 15 select 
 16     fname as 姓名,
 17     fage as 年龄,
 18     fgender as 性别,
 19     fid as 学号
 20 from MyStudent
 21 where fgender= and fage>=20 and fage<=24
 22 
 23 select 
 24     fname 姓名,
 25     fage 年龄,
 26     fgender 性别,
 27     fid 学号
 28 from MyStudent
 29 where fgender= and fage>=20 and fage<=24
 30 
 31 select 
 32     姓   名=fname, --对于那些非法的命名方式,可以使用[]或者‘‘包含起来。
 33     年龄=fage,
 34     性别=fgender,
 35     学号=fid
 36 from MyStudent
 37 where fgender= and fage>=20 and fage<=24
 38 
 39 --------Select语句可以单独使用
 40 select A=你好,B=世界
 41 
 42 select getdate()
 43 
 44 sp_helptext  sp_rename
 45 
 46 
 47 select * from TblTeacher
 48 
 49 --对已经通过select语句查询出的结果集,进行去除重复操作distinct
 50 select 
 51     distinct
 52     ttname,
 53     ttgender,
 54     ttage
 55 from tblteacher
 56 
 57 select 
 58     distinct
 59     ttid,
 60     ttname,
 61     ttgender,
 62     ttage
 63 from tblteacher
 64 
 65 select distinct ttname from tblteacher
 66 -----------------------------------------------------
 67 ---top,获取查询出的结果集中的前n条。
 68 --order by 进行排序。所以,一般使用top的时候,必须配合排序一起使用才有意义。
 69 select * from MyStudent
 70 
 71 
 72 --查找年龄最小的前5名同学
 73 --asc 表示升序排序,从小到大。
 74 --desc表示降序排序,从大到小。
 75 --默认不写,表示是asc,升序。
 76 select top 5 * from MyStudent order by fage asc,fmath desc
 77 select top (5*3) * from MyStudent order by fage asc,fmath desc
 78 
 79 --------\
 80 select top 5 percent * from MyStudent order by fage asc,fmath desc
 81 
 82 ------------聚合函数---------------------
 83 --求总和的聚合函数
 84 select * from TblScore
 85 
 86 select
 87     数学成绩总分=sum(tMath)
 88 from TblScore
 89 
 90 ---求最大值
 91 select
 92     数学成绩总分=max(tMath)
 93 from TblScore
 94 --求最小值
 95 select
 96     数学成绩总分=min(tMath)
 97 from TblScore
 98 --求平均值
 99 --注意:求平均值得时候如果表中的列的数据类型是整数,那么最后计算出的平均值可能有问题
100 --因为    整数/整数 最后得到的结果还是整数。解决:把其中一个数据转换为小数。 值*1.0
101 select
102     数学成绩总分=avg(tMath)
103 from TblScore
104 
105 
106 ---请查询出,所有那些tmath的值大于90分的人的个数。
107 --count()聚合函数,最后返回的是所查询出的记录的条数。
108 --如果没有查询出任何数据,那么count()聚合函数返回值是0
109 select count(*) from TblScore where tmath>750
110 
111 select * from TblScore
112 
113 --聚合函数不统计null值。
114 select count(tEnglish) from TblScore
115 
116 select count(*) from TblScore
117 select count(1) from TblScore
118 
119 
120 select
121     数学成绩总分=sum(tEnglish)
122 from TblScore
123 
124 --注意:avg()聚合函数同样不统计null值。
125 select
126     数学成绩总分=avg(tEnglish)
127 from TblScore
128 
129 select * from MyStudent
130 select
131     max(FBirthday) as maximum,
132     min(FBirthday) as minimum
133 from MyStudent
134 where fgender=
135 
136 
137 select * from TblScore;
138 select 
139     tsid,
140     tenglish,
141     tmath,
142     个人平均分=(tEnglish+tMath)/2
143 from TblScore
144 order by 个人平均分 desc
145 
146 
147 
148 select * from TblTeacher
149 
150 select count(ttname) from TblTeacher
151 
152 select COUNTDISTINCT(ttname) from TblTeacher
153 
154 
155 --查询没有及格的学生的学号
156 select * from TblScore;
157 select tsid from TblScore where tmath<60
158 
159 
160 --查询年龄在20-30岁之间的男学生
161 select * from TblStudent where tsage>=20 and tsage<=30 and tsgender=
162 select * from TblStudent where tsage between 20 and 30 and tsgender=
163 
164 
165 --Between…and …    在...之间,(闭区间,包含两个端点值)
166 
167 --查询年龄在20-30岁之间的男学生
168 
169 --查询math成绩在80-90分之间的所有学生
170 select * from TblScore;
171 select * from TblScore where tmath between 80 and 90
172 
173 -----------------------------------------------------------------
174 select * from TblStudent
175 
176 select * from TblClass
177 
178 select * from TblStudent where tsclassid=5 or tsclassid=3 or tsclassid=1
179 
180 select * from TblStudent where tsclassid in (5,3,1) --其实这里使用in()等价于使用or
181 
182 select * from TblStudent where tsclassid in (3,4,5)
183 
184 --如果使用in的时候,小括号中的值恰好是一个连续的值,那么建议改成>=  and <=的方式,这样可以很好地使用索引。可以提高性能。
185 select * from TblStudent where tsclassid>=3 and tsclassid<=5
186 
187 --------------------------模糊查询---------------------------------------
188 select * from TblStudent
189 
190 -- %  表示可以匹配任意多个任意字符。
191 --查询所有以"张"字开头的人 
192 select * from TblStudent where tsname like 张%
193 
194 --查询那些所有以"张"字开头的,并且是两个字的人。
195 select * from TblStudent where tsname like 张% and len(tsname)=2
196 
197 --ctrl + R
198 
199 --  _   表示可以匹配任意的单个字符。_ 必须匹配一个字符,没有字符不行
200 
201 select * from TblStudent where tsname like 张_
202 
203 --  []  这里的含义与正则表达式中的含义是一致的,都是表示在中括号的范围内任意匹配一个字符。
204 
205 select * from TblStudent where tsname like _[a-z0-9]_
206 
207 -- ^  在中括号中的时候,表示非的意思。
208 
209 select * from TblStudent where tsname like _[^a-z0-9]_
210 
211 --当希望通配符只表示一个普通字符的时候,此时应该将通配符放到[]中。
212 select * from TblStudent where tsname like %[_]%   --[[]
213 
214 select * from TblStudent where tsname like 张% 
215 
216 select * from TblStudent where tsname not like 张% 
217 
218 select * from TblStudent where tsname like [^张]%
219 ---------------------------------------对于数据库中的null值处理--------------------------------
220 --数据库中的null值比较特殊,不能用=或者<>来进行比较。
221 select * from TblStudent where tsage=null
222 
223 select * from TblStudent where tsage<>null
224 
225 ------------------------------------------------
226 --数据库中的null值,比较的时候必须使用is null或者是is not null来进行判断,绝对不能直接使用=或者<>
227 select * from TblStudent where tsage is null
228 --isnull()
229 
230 select * from TblStudent where tsage is not null
231 
232 select tsage+10 from TblStudent where tsid=1
233 
234 --null值与任何其他值计算后结果还是null值。
235 select tsage+10 from TblStudent where tsid=2
236 
237 ----------------------------- 排序 order by-----------------
238 
239 --1>使用方式:
240 --order by 列1 asc,列2 desc
241 --2>查询语句的基本结构
242 
243 
244 --select
245 --    选择列   -------------【三】
246 --from 表    -------------【一】
247 --where 条件(其实就是对表中数据的筛选,主要对数据行的筛选)  -------------【二】
248 --order by 列 (对数据先通过where条件进行筛选,然后对筛选后的数据再进行排序)-------------【四】
249 --无论一条查询语句有多么复杂,一般情况下,order by 语句永远都是最后执行。
250 select * from TblScore
251 
252 --当一个查询使用了order by 以后,那么这个查询,查询出的结果就是一个有序的结果,
253 --既然是一个有序的结果,就不是一个集合了(集合是无序的),所以如果一个查询希望被另外一个查询使用,那么不要使用order by(除非还同时使用了top)
254 select
255     tscoreId,
256     tsid,
257     tenglish,
258     tmath
259     --avgscore=(tEnglish+tMath)/2
260 from tblscore
261 order by (tEnglish+tMath)/2 desc
262 
263 ---------------------------------------------Group by 数据分组 --------------------------
264 --1.分组的目的,就是为了汇总、统计。================================================
265 --2.聚合函数。刚才所说的聚合函数其实就是把整个表中的数据作为”一组“,来进行统计汇总。
266 --聚合函数使用的时候一定会配合分组(gourp by )来使用,如果使用聚合函数时,没用分组,那么意义不大。
267 --聚合函数在使用的时候一定会分组,即便不写group by 语句,其实也是默认把整个表中的数据作为”一个组“来使用,进行统计.
268 
269 select * from TblStudent
270 
271 -- 当聚合函数与group by 分组语句一起使用的时候,其实这个聚合函数会将分组后的每一组的记录数据,进行聚合。
272 --group by 语句最终执行完毕后,分了几组,那么聚合函数会对每一组都进行聚合统计。
273 select
274     tsgender,
275     count(*)
276 from TblStudent
277 group by tsgender

 

MSSQL DBOtherSQL

标签:

原文地址:http://www.cnblogs.com/Dr-Hao/p/5232530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!