Sql 使用case-when条件时不从表返回行

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, '') = ''; 带有条件的过滤器,其中条

我有一个基本问题。 当条件为true时,我想从表中返回数据,但当条件为true时,我得到的是空行,但我不希望这样

这是我的例子:

 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
是一个常量,那么查询将永远不会执行。谢谢这是一个解决方案。