Sql where子句中的用例

Sql where子句中的用例,sql,case,where,Sql,Case,Where,我试图将case语句添加到查询中的where条件中。 我的问题如下所示- select * from table_name where if(id==1) then col1 = 0 else col1 is null 如上面的查询所示,如果id-==0,我希望在where条件中添加col1=0,否则我希望col1在where条件中为null。 我试过了 但是语法是不正确的。 有什么方法可以做到这一点吗?我不确定您的要求,但如果下面的内容满足您的要求,您可以试试 SELEC

我试图将case语句添加到查询中的where条件中。 我的问题如下所示-

select * from table_name
where
if(id==1) then col1 = 0
else           col1 is null
如上面的查询所示,如果id-==0,我希望在where条件中添加col1=0,否则我希望col1在where条件中为null。 我试过了

但是语法是不正确的。
有什么方法可以做到这一点吗?

我不确定您的要求,但如果下面的内容满足您的要求,您可以试试

SELECT *
FROM table_name
WHERE isnull(id, '')= CASE(id)
               WHEN 1
               THEN 0
               ELSE ''
           END;
select * from table_name
where 
isnull(col1,'') = case when id = 1 then   0
else '' end
也尝试

select * from table_name
where 
( col1 = 0 and id = 1) OR ( col1 is null and id<>1 )
从表名称中选择*
哪里
(col1=0且id=1)或(col1为null且id1)

我不确定您的要求,但如果以下内容满足您的要求,您可以尝试

select * from table_name
where 
isnull(col1,'') = case when id = 1 then   0
else '' end
也尝试

select * from table_name
where 
( col1 = 0 and id = 1) OR ( col1 is null and id<>1 )
从表名称中选择*
哪里
(col1=0且id=1)或(col1为null且id1)

我认为您正在寻找一个解决方案。如下所示,可用于填写特定列。但在执行此操作时,也需要选择其他列

select 
case when id = 1 then 0 else null end as col1
from table_name

我想你是在寻找一个解决问题的案例。如下所示,可用于填写特定列。但在执行此操作时,也需要选择其他列

select 
case when id = 1 then 0 else null end as col1
from table_name

我想这就是你想要的:

select case when id = 1 then 0 else null end as col1, * from table_name

我想这就是你想要的:

select case when id = 1 then 0 else null end as col1, * from table_name
为什么不
Where(id=1和col1=0)或(id=1和col1为NULL)
?为什么不
Where(id=1和col1=0)或(id=1和col1为NULL)
?与NULL的相等比较(id=NULL)我认为它不起作用与NULL的相等比较(id=NULL)我认为它不起作用