SQL中case,when,then,end的用法(…

来源:互联网 发布:笑郭网络验证4.0破解 编辑:程序博客网 时间:2024/05/17 09:31
转载自:http://blog.csdn.net/chaojishuaigeli/article/details/8165802
  1. create database demo  
  2. use demo  
  3.   
  4. create table [user 
  5.  
  6.     [uId] int identity(1,1) primary key 
  7.     [namevarchar(50),  
  8.     [levelint  --1骨灰2大虾3菜鸟  
  9.  
  10. insert into [user(name,levelvalues('犀利哥',1)  
  11. insert into [user(name,levelvalues('小月月',2)  
  12. insert into [user(name,levelvalues('芙蓉姐姐',3)  
  13.   
  14.   
  15.   
  16. --case end  相当于switch case  
  17. --then后面的返回值类型必须一致  
  18. select [name],  
  19.     case [level 
  20.         when then '骨灰'  
  21.         when then '大虾'  
  22.         when then '菜鸟'  
  23.     end as '等级'  
  24. from [user 
  25.   
  26. use MySchool  
  27. select from score  
  28. --case end第二种用法,相当于多重if语句  
  29. select studentId,  
  30.     case  
  31.         when english >=90 then '优'  
  32.         when english >=80 and english <90 then '良'  
  33.         when english >=70 and english 80 then '中'  
  34.         when english >= 60 and english 70 then '可'  
  35.         else '差'  
  36.     end as '成绩'  
  37. from score  
  38. order by english  
  39.   
  40.   
  41. --表中有A C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。  
  42. select   
  43.     case  
  44.         when a>b then  
  45.         else  
  46.     end 
  47.     case      
  48.         when b>c then  
  49.         else  
  50.     end  
  51. from  
  52.   
  53.   
  54. --练习2  
  55. create table test  
  56.  
  57.     number varchar(10),  
  58.     amount int  
  59.  
  60. insert into test(number,amount) values('RK1',10)  
  61. insert into test(number,amount) values('RK2',20)  
  62. insert into test(number,amount) values('RK3',-30)  
  63. insert into test(number,amount) values('RK4',-10)  
  64.   
  65. select number,  
  66.     case   
  67.         when amount then amount  
  68.         else  
  69.     end as '收入' 
  70.     case  
  71.         when amount then abs(amount)  
  72.         else  
  73.     end as '支出'  
  74. from test  
  75.   
  76.   
  77. --有一张表student0,记录学生成绩  
  78. use demo  
  79. CREATE TABLE student0 (name nvarchar(10),subject nvarchar(10),result int 
  80. INSERT INTO student0 VALUES ('张三','语文',80)  
  81. INSERT INTO student0 VALUES ('张三','数学',90)  
  82. INSERT INTO student0 VALUES ('张三','物理',85)  
  83. INSERT INTO student0 VALUES ('李四','语文',85)  
  84. INSERT INTO student0 VALUES ('李四','数学',92)  
  85. INSERT INTO student0 VALUES ('李四','物理',null 
  86.   
  87. select from student0  
  88.   
  89. select [name],  
  90.     isnull(sum(case subject   
  91.         when '语文' then result  
  92.     end),0) as '语文' 
  93.     isnull(sum(case subject  
  94.         when '数学' then result  
  95.     end),0) as '数学' 
  96.     isnull(sum(case subject  
  97.         when '物理' then result  
  98.     end),0) as '物理'  
  99. from student0  
  100. group by [name 
  101.   
  102.   
  103.   
  104. --子查询  
  105. use myschool  
  106. select sName from (select from student) as  
  107.   
  108.   
  109.   
  110. select 1,(select sum(english) from score) as '和',(select avg(sAge) from student) as '平均年龄'  
  111.   
  112. --查询高一一班所有的学生  
  113. select from student where sClassId   
  114. (select cId from class where cName='高一一班' 
  115.   
  116. --查询高一一班  高二一班 所有的学生  
  117. --子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后  
  118. --子查询跟在比较运算符之后,要求子查询只返回一个值  
  119. select from student where sClassId  
  120. (select cId from class where cName in ('高一一班','高二一班'))  
  121.   
  122.   
  123. select from student where sClassId in  
  124. (select cId from class where cName in ('高一一班','高二一班'))  
  125.   
  126.   
  127. --查询刘关张的成绩  
  128. select from score where studentId in  
  129. (select sId from student where sName in ('刘备123','关羽','张飞'))  
  130.   
  131. select from student  
  132.   
  133. --删除刘关张  
  134. delete from score where studentId in  
  135. (select sId from student where sName in ('刘备123','关羽','张飞'))  
  136.   
  137.   
  138. --实现分页  
  139. --最近入学的3个学生  
  140. select top from student   
  141. order by sId desc  
  142.   
  143. --查询第4到6个学生  
  144. select top from student  
  145. where sId not in (select top sId from student order by sId desc 
  146. order by sId desc  
  147.   
  148.   
  149. --查询7到9个学生  
  150. select top from student  
  151. where sId not in (select top sId from student order by sId desc 
  152. order by sId desc  
  153.   
  154. --查询第n页的学生  
  155. select top from student  
  156. where sId not in (select top (5*(2-1)) sId from student order by sId desc 
  157. order by sId desc  
  158.   
  159.   
  160. select from student  
  161.   
  162.   
  163. --sql 2005中的分页  
  164.   
  165. select from   
  166. (select row_number() over(order by sId descas num,* from student) as  
  167. where num between and  
  168.   
  169.   
  170. select from   
  171. (select row_number() over(order by sId descas num,* from student) as  
  172. where num between and  
  173.   
  174. select from   
  175. (select row_number() over(order by sId descas num,* from student) as  
  176. where num between and  
  177.   
  178.   
  179. select from   
  180. (select row_number() over(order by sId descas num,* from student) as  
  181. where num between 3*(3-1) and 3*3  
  182.   
  183.   
  184.   
  185. --表连接  
  186. --交叉连接cross join  
  187. select from student   
  188. cross join class  
  189.   
  190.   
  191. --内连接inner join...on...  
  192. select from student  
  193. inner join class on sClassId=cId  
  194.   
  195. select from class  
  196.   
  197. --查询所有学生的姓名、年龄及所在班级  
  198. select sName,sAge,cName,sSex from student  
  199. inner join class on sClassId cId  
  200. where sSex ='女'  
  201. --查询年龄超过20岁的学生的姓名、年龄及所在班级  
  202. select sName,sAge,cName from class  
  203. inner join student on sClassId cId  
  204. where sAge 20  
  205.   
  206. --外连接  
  207. --left join...on...  
  208. select sName,sAge,cName from class  
  209. left join student on sClassId cId 
原创粉丝点击