Sql 使用case-when条件时不从表返回行
我有一个基本问题。 当条件为true时,我想从表中返回数据,但当条件为true时,我得到的是空行,但我不希望这样 这是我的例子:Sql 使用case-when条件时不从表返回行,sql,postgresql,Sql,Postgresql,我有一个基本问题。 当条件为true时,我想从表中返回数据,但当条件为true时,我得到的是空行,但我不希望这样 这是我的例子: select case when coalesce(var, '') != '' then (select id from user) end 当var不是“”时,我得到数据,bat,当不是我时,得到null 带有的过滤器,其中条件: select id from "user" where coalesce(var, '') = ''; 带有条件的过滤器,其中条
select case when coalesce(var, '') != '' then (select id from user) end
当var不是“”时,我得到数据,bat,当不是我时,得到null
带有
的过滤器,其中条件:
select id from "user"
where coalesce(var, '') = '';
带有条件的过滤器,其中
条件:
select id from "user"
where coalesce(var, '') = '';
我认为您只需要过滤掉where
子句中返回的NULL
s:
select coalesce(var, id)
from user
where coalesce(var, id) is not null
coalesce
返回字段列表中的第一个非空值。当两个条件都不满足时,将返回一个NULL
行,并且where
子句将过滤掉这些行。我认为您只需过滤掉where
子句中返回的NULL
s即可:
select coalesce(var, id)
from user
where coalesce(var, id) is not null
coalesce
返回字段列表中的第一个非空值。当两个条件都不满足时,将返回一个NULL
行,并且where
子句将过滤掉这些条件。您似乎要求:
select id
from "user"
where var <> '';
选择id
来自“用户”
其中var为“”;
您不需要将NULL
s作为特例处理,因为当var
为NULL
时,
将不会返回任何行,您似乎在要求:
select id
from "user"
where var <> '';
选择id
来自“用户”
其中var为“”;
您不需要将NULL
s作为特例处理,因为
在var
为NULL
时将不会返回任何行,我甚至不确定是否会运行此查询。列var
属于哪个表?我不想在coalesce(var')!=''时执行我的select id from user
代码>现在更好理解了吗?我甚至不确定这个查询是否会运行。列var
属于哪个表?我不想在coalesce(var')!=''时执行我的select id from user
代码>现在更好理解了吗?我不想执行从“用户”中选择id
当is合并(var')!=''时代码>。我想我不明白。那就换掉=代码>带有=
。如果var
是一个常量,那么查询将永远不会执行。感谢这是一个解决方案。我不想在coalesce(var')时执行select id from“user”
代码>。我想我不明白。那就换掉=代码>带有=
。如果var
是一个常量,那么查询将永远不会执行。谢谢这是一个解决方案。