gpt4 book ai didi

sql - PostgreSQL 中的 CAST

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:18 25 4
gpt4 key购买 nike

我有这个查询,我需要在 t.delete_req='1' 和 t.delete_req='0' 时设置 'yes' 和 'no'

SELECT t.id,t.activity_id,t.type,t.sub_type,t.name,t.description,t.source, 
CASE t.delete_req WHEN t.delete_req='1' THEN 'Yes' WHEN t.delete_req='0' THEN 'No' END from "activities" "t"

它抛出错误并说存在一些转换问题。以下是错误。

ERROR:  operator does not exist: character = boolean
LINE 2: CASE t.delete_req WHEN t.delete_req='1' THEN 'Yes' WHEN t.de...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

现在我试着用这种方式转换它,//CAST(t.delete_req AS char)='1'

SELECT t.id,t.activity_id,t.type,t.sub_type,t.name,t.description,t.source, 
CASE t.delete_req
WHEN CAST(t.delete_req AS char)='1' THEN 'Yes'
WHEN CAST(t.delete_req AS char)='0' THEN 'No' END from "activities" "t"

但我仍然无法让它工作。错误是一样的。

ERROR:  operator does not exist: character = boolean

在表中,t.delete_req 是一个字符,定义如下。

delete_req character(1) NOT NULL DEFAULT '0'::bpchar,

最佳答案

您必须稍微更改语法(为清楚起见格式化):

select
t.id,t.activity_id,t.type,t.sub_type,t.name,t.description,t.source,
case t.delete_req
when '1' then 'Yes'
when '0' then 'No'
end
from activities as t

select
t.id,t.activity_id,t.type,t.sub_type,t.name,t.description,t.source,
case
when t.delete_req = '1' then 'Yes'
when t.delete_req = '0' then 'No'
end
from activities as t

关于sql - PostgreSQL 中的 CAST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18887442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com