3

我有这个查询,当 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,
4

1 回答 1

1

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

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
于 2013-09-19T06:31:52.770 回答