postgresql 中的 case-statement,用于根据列是否存在来执行不同的事情



我正在尝试在 postgres 中做一个 Case-语句,根据列是否存在来做不同的事情,

CASE 
WHEN select exists (select * from information_schema.columns where 
table_name = 'mytable' and column_name = 'mycolumnt')
THEN select mycolumn from mytable where mycolumnt = true
ELSE select mycolumn from mytable
END

执行案例语句后,我得到以下错误

错误

:语法错误在"大小写"处或附近

有什么提示我做错了什么吗?我的帖子知识非常基本。

查询应以SELECT开头。喜欢:

SELECT mycolumn
FROM mytable
WHERE CASE WHEN exists (
select * 
from information_schema.columns 
where table_name = 'mytable' and column_name = 'mycolumnt')
THEN mycolumnt = true
ELSE true
END

另外,我建议您始终使用mycolumnt,只需在没有翻译时将其设置为false即可。这会将您的查询简化为:

SELECT mycolumn
FROM mytable
WHERE mycolumnt = true

相关内容

最新更新