我正在尝试创建一个只会在符合某些条件的情况下添加AND子句的查询。
这就是我所追求的:
SELECT DISTINCT
id
name
active
FROM team
WHERE id = 1234
IF team.active = 'y'
AND team.id IN
{
query
}
ELSEIF team.active = 'n'
AND team.id IN
{
query
}
但我不确定如何在SQL中执行此操作。有什么建议吗?
答案 0 :(得分:5)
SELECT DISTINCT
id
name
active
FROM team
WHERE id = 1234
AND (team.active = 'y'
AND team.id IN
{
query
})
OR
(team.active = 'n'
AND team.id IN
{
query
})
使用AND和OR连接词来创建逻辑。
答案 1 :(得分:3)
你可以像这样使用布尔逻辑
SELECT ...
FROM team
WHERE id = 1234
AND (
(team.active = 'y' AND team.id IN query)
OR
(team.active = 'n' AND team.id IN query)
)
我是否错过了两个分支的区别?
答案 2 :(得分:-1)
您可以使用decode语句来实现结果
SELECT DISTINCT
id
name
active
FROM team
WHERE id = 1234 and
team.id in (
decode (team.active,'y',(query),'n',(query))
您还可以使用案例陈述