本文将详细介绍下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,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

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 lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。