没有运算符匹配给定的名称和参数类型:需要转换什么?

时间:2018-01-12 23:17:38

标签: postgresql pattern-matching case

我收到错误,我无法弄清楚原因。我知道这个错误告诉我要投一个类型,但我不确定是什么?

CASE的哪一部分是运营商?

ERROR:  operator does not exist: character varying = boolean
LINE 6:              WHEN lower(foo.name) SIMILAR TO '%(foo
                    ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我的查询:

SELECT foo.name,
       bar.city,
       MAX(foo.total - bar.tax_amount),
       CASE bar.name
            WHEN lower(foo.name) SIMILAR TO '%(foo|bar|baz)%' THEN true
            ELSE false
        END
        ....
GROUP BY foo.name, bar.city;

2 个答案:

答案 0 :(得分:1)

我相信你只想要:

SELECT foo.name,
       bar.city,
       MAX(foo.total - bar.tax_amount),
       (CASE WHEN lower(foo.name) SIMILAR TO '%(foo|bar|baz)%' THEN true
            ELSE false
        END)
        ....
GROUP BY foo.name, bar.city;

CASE表达式有两种形式。一个人搜索给定表达式的值。这会在CASE之后立即使用列或表达式。第二个搜索各种表达式,停在第一个表达式。这会在WHEN之后立即使用CASE子句。

甚至更简单:

SELECT foo.name, bar.city,
       MAX(foo.total - bar.tax_amount),
       (lower(foo.name) SIMILAR TO '%(foo|bar|baz)%') 
        ....
GROUP BY foo.name, bar.city;

不需要CASE

答案 1 :(得分:0)

您的CASE expression语法错误为like @a_horse commented

但在这种特殊情况下,您根本不需要CASE boolean结果。只是:

foo.name ~* '(foo|bar|baz)'

就是这样。

~* being the case-insensitive regular expression match operator.

从不使用SIMILAR TO