本文将详细介绍下Case函数的用法感兴趣的你可以参考下,或许对你有所帮助
1.Case函数的用法
(1)使用类似:switch-case与if-else if。
(2)语法:
case [字段]
when 表达式 then 显示数据
when 表达式 then 显示数据
else 显示数据
end
(3)百分制转换素质教育
1)如图:我们要将显示的数据转换成ABCDE,规则是90分以上显示A,80分以上显示B,以此类推。
2)执行的SQL语句是:
代码如下:
Select ID,TestBase,
Case
When testBase>=90 then ‘A'
When testBase>=80 then ‘B'
When testBase>=70 then ‘C'
When testBase>=60 then ‘D'
Else ‘E' end as testBaseLevel,
testBeyond,testDate from Score
3)最后的执行结果如图所示:
(4)注意:
1)写case对应的就写上end。
2)end后面跟别名(case与end之间相当于一个字段(对象))
(5)和C#的switch-语法作比较
1)C#
Switch(变量)
{
Case 常量1:结果1;break;
Case 常量2:结果2;break;
Default :默认结果;break;
}
2) SQL
SQL语法我在上面写了,可以对比看一下。
(6)对应的if-else if语法
1) C#
If(表达式1) {结果1;}
else if(表达式2) {结果2;}
else {默认结果;}
2)SQL Server
case
when 表达式1 then 显示1
when 表达式2 then 显示2
else 默认显示
end
3)举例说明,如果我们存放性别的时候在数据库中是用f,m标识的,现在我们想要用男女标识,SQL语句如下:
代码如下:
Select ID,Name,stuSex,
case
when stuSex='m' then ‘男'
when syuSex='f' then ‘女'
else ‘其它'
end as stuSexType,
stuDate from Student。
(7)练习案例:
1)在数据库中执行这段代码:
代码如下:
use Test
go
create table PracticeTest
(
number varchar(10),
amount int
)
insert into PracticeTest(number,amount) values('RK1',10)
insert into PracticeTest(number,amount) values('RK2',20)
insert into PracticeTest(number,amount) values('RK3',-30)
insert into PracticeTest(number,amount) values('RK4',-10)
2)实现的效果如下:
3)可以看出,首先select中应该有三个字段,并且将数据大于0的放到收入中,那么另一个为0,并且将小于0的放到支出里面,另一个为0,下面我们写实现的SQL语句:
代码如下:
select number as 单号,
case
when amount>0 then amount
else 0
end as 收入,
case
when amount else 0
end as 支出
from PracticeTest
(8)一道面试题的练习:
1)如图:我们写出下面执行的代码,数据库大家自己建或者我在下面附加脚本了,大家制药执行一下即可:
2)执行的SQL语句:
代码如下:
create table Score
(
学号 nvarchar(10),
课程 nvarchar(10),
成绩 int
)
insert into Score values('0001','语文',87)
insert into Score values('0002','数学',79)
insert into Score values('0003','英语',95)
insert into Score values('0004','语文',69)
insert into Score values('0005','数学',84)
3)实现功能的SQL语句的书写
代码如下:
select 学号,sum(
case
when 课程='语文' then 成绩
else 0
end) as 语文,sum(
case
when 课程='数学' then 成绩
else 0
end) as 数学,sum(
case
when 课程='英语' then 成绩
else 0
end) as 英语
from score group by 学号
相信自己,你就是下一个奇迹!

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器