gpt4 book ai didi

SQL Server : CASE statement in the where clause

转载 作者:行者123 更新时间:2023-12-03 02:28:11 25 4
gpt4 key购买 nike

我使用的是 SQL Server,如何在 SQL 语句的 where 子句中使用 CASE 语句?

我想重写这个查询:

select * 
from Persons P
where P.Age = 20
and P.FamilyName in (select Name
from AnotherTable)

使用case语句。我想要它,所以第二个条件

(P.FamilyName in ....)

仅当 CheckFamilyName 为 true 时执行。

类似这样的事情:

select * 
from Persons P
where P.Age = 20
case when CheckFamilyName= true
then
and P.FamilyName in (
select Name
From AnotherTable)
else
end

SQL 中的 case 语句如何工作?

最佳答案

where   P.Age = 20
and
(
not CheckFamilyName
or
P.FamilyName in (select Name From AnotherTable)
)

关于SQL Server : CASE statement in the where clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com