gpt4 book ai didi

sql - 在 WHERE 条件下加倍

转载 作者:行者123 更新时间:2023-12-04 18:23:29 27 4
gpt4 key购买 nike

是否可以在 where 语句 @Age 中执行类似这里发生的事情?参数?

 @AGE Varchar(8)

SELECT NAME FROM TABLE WHERE
(
If @AGE='man'
then (AGE = 'man' or AGE = 'boy')
else (AGE = 'man')
)
AND City IS NULL

最佳答案

是的,像这样:

SELECT NAME FROM TABLE WHERE 
((@AGE='man'
AND (AGE = 'man' or AGE = 'boy'))
OR
(@AGE <> 'man'
AND (AGE = 'man')))
AND City IS NULL

注意条件 @AGE <> 'man'可能需要根据@AGE 变量是否可以为空进行调整。

我回答了一个非常相似的问题 here .

This is a very common technique in a WHERE clause. If you want to apply some "IF" logic in the WHERE clause all you need to do is add the extra condition with an boolean AND to the section where it needs to be applied.

关于sql - 在 WHERE 条件下加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10186558/

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