gpt4 book ai didi

sql - 根据不同的情况在 where 子句中获取记录

转载 作者:行者123 更新时间:2023-12-02 08:16:40 25 4
gpt4 key购买 nike

我正在创建一个带有参数的过程,并且基于一个参数,我必须在 where 子句示例中放置一个不同的过滤器:

@para1 
as

Select a,b,c from z
where @para1 = 0 then a = a
and @para1 = 1 then a between 1 to 10
and @para1 = 2 then a between 11 and 20

最佳答案

使用AND/OR逻辑

SELECT a,b,c
FROM z
WHERE @para1 = 0 -- to pull all records when @para1 = 0
OR ( @para1 = 1
AND a BETWEEN 1 AND 10 ) -- To pull records BETWEEN 1 AND 10 when @para1 = 1
OR ( @para1 = 2
AND a BETWEEN 11 AND 20 ) -- To pull records BETWEEN 11 AND 20 when @para1 = 2

关于sql - 根据不同的情况在 where 子句中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41355915/

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