PostgreSQL - 使用大小写将带引号的字符串转换为不同的字符串?
PostgreSQL - Converting a string with quotes to different string using case?
所以希望这不是一个超级困难的问题。我环顾四周,但找不到答案。基本上我在只读数据库中有一个 table,我试图使用 case 语句将存储 JSON 值(我认为?)的列转换为更美观的东西。
table 显示电子邮件,以及他们是否订阅邮件列表的状态。
基本上我的查询看起来像这样
select
f.data as "Email",
(
case f.status
when '{"value":true}' then 'Yes'
when '{"value":false}' then 'No'
else NULL
end
) as "Subscribed"
from fields f
当我在我的示例页面中 运行 它设置为 POSTGRES 11 时它工作得很好但是当我 运行 它在 Metabase 上时,我得到一个错误 "ERROR: operator does not exist: json = unknown" 并且我'我对如何进行感到困惑。
如有任何帮助,我们将不胜感激。
该错误意味着 f.status
是 json
类型,而您正试图将其与字符串进行比较,但这是行不通的。
您可以尝试以下方法(相关documentation):
case f.status->>'value'
when 'true' then 'Yes'
when 'false' then 'No'
end
as "Subscribed"
所以希望这不是一个超级困难的问题。我环顾四周,但找不到答案。基本上我在只读数据库中有一个 table,我试图使用 case 语句将存储 JSON 值(我认为?)的列转换为更美观的东西。
table 显示电子邮件,以及他们是否订阅邮件列表的状态。
基本上我的查询看起来像这样
select
f.data as "Email",
(
case f.status
when '{"value":true}' then 'Yes'
when '{"value":false}' then 'No'
else NULL
end
) as "Subscribed"
from fields f
当我在我的示例页面中 运行 它设置为 POSTGRES 11 时它工作得很好但是当我 运行 它在 Metabase 上时,我得到一个错误 "ERROR: operator does not exist: json = unknown" 并且我'我对如何进行感到困惑。
如有任何帮助,我们将不胜感激。
该错误意味着 f.status
是 json
类型,而您正试图将其与字符串进行比较,但这是行不通的。
您可以尝试以下方法(相关documentation):
case f.status->>'value'
when 'true' then 'Yes'
when 'false' then 'No'
end
as "Subscribed"