sql中case语句用法举例说明(SQL中CASE控制语句用法)
本文目录
SQL中CASE控制语句用法
DECLARE @var int;CASE @var WHEN 1 THEN ’var is 1’ WHEN 2 THEN ’var is 2’ELSE ’var is not 1 or 2’ END
sql中case的用法
case 列 when 值1 then 值2 --当列的值等于值1 就返回值2 when 值3 then 值4 --当列的值等于值3 就返回值4 endcase when 列=值1 then 值2 --当列的值等于值1 就返回值2 when 列=值3 then值4 --当列的值等于值3 就返回值4end
关于SQL中的CASE的用法,请用学生成绩表来说明
eclare @t table(id int, name char(10),class char(15),sex char(10))insert into @t select 1,’alex’,’1’,’man’ union allselect 2,’kelly’,’2’,’man’ union allselect 3,’lily’,’1’,’woman’ union allselect 4,’judy’,’1’,’woman’ union allselect 5,’tom’,’3’,’man’ union allselect 6,’cherly’,’2’,’woman’ union allselect 7,’cherly’,’3’,’woman’ union allselect 8,’romeo’,’2’,’man’select * from @tselect class,sex ,(case when sex=’man’ then count(sex) when sex=’woman’ then count(sex) end) 人数 from @t group by sex,class(所影响的行数为 8 行)id name class sex ----------- ---------- --------------- ---------- 1 alex 1 man 2 kelly 2 man 3 lily 1 woman 4 judy 1 woman 5 tom 3 man 6 cherly 2 woman 7 cherly 3 woman 8 romeo 2 man (所影响的行数为 8 行)class sex 人数 --------------- ---------- ----------- 1 man 11 woman 22 man 22 woman 13 man 13 woman 1(所影响的行数为 6 行)
更多文章:

img文件如何打开(img文件怎么打开如何打开img格式文件)
2025年3月3日 20:10

dominion energy(求星际争霸:母巢之战 英文原文剧本)
2025年3月13日 12:30

trollface(史上最贱小游戏3(trollface quest 3)第15关的攻略)
2025年2月17日 23:50

linux解压rar压缩包(LINUX怎么解压rar格式的文件,还有怎么安装软件)
2025年4月10日 10:20

filename函数的用法(Excel用公式函数提取文件名)
2025年3月14日 14:40

premiere教程下载?哪里可以下载3DMAX VRAY室外渲染教程!要可以用的哦!谢谢!
2025年4月12日 08:00

jqgrid隐藏列(jqgrid控制列的隐藏显示,怎么使table的宽度不变)
2025年2月13日 21:30

网站模板企业官网(做一个企业网站多少钱,如何建自己的企业官网 )
2025年4月7日 14:20

choices是什么意思(plag.choices.help.各是什么意思)
2025年3月13日 16:50