【问题标题】:PostgreSQL: ERROR: operator does not exist: integer = character varyingPostgreSQL:错误:运算符不存在:整数 = 字符变化
【发布时间】:2014-06-30 15:21:12
【问题描述】:

这里我正在尝试创建如下示例所示的视图:

例子:

 create view view1
 as 
 select table1.col1,table2.col1,table3.col3
 from table1 
 inner join
 table2 
 inner join 
 table3
 on 
 table1.col4 = table2.col5 
 /* Here col4 of table1 is of "integer" type and col5 of table2 is of type "varchar" */
 /* ERROR: operator does not exist: integer = character varying */
 ....;

注意:在 sql server 中执行相同的查询,但在 postgreSQL 中出现上述错误。

【问题讨论】:

    标签: postgresql casting integer varchar


    【解决方案1】:

    我认为它可以准确地告诉你哪里出了问题。您不能将整数与 varchar 进行比较。 PostgreSQL 是严格的,不会为你做任何神奇的类型转换。我猜 SQLServer 会自动进行类型转换(这是一件坏事)。

    如果您想比较这两种不同的野兽,您必须使用转换语法 :: 将一个转换为另一个。

    类似的东西:

    create view view1
    as 
    select table1.col1,table2.col1,table3.col3
    from table1 
    inner join
    table2 
    inner join 
    table3
    on 
    table1.col4::varchar = table2.col5
    /* Here col4 of table1 is of "integer" type and col5 of table2 is of type "varchar" */
    /* ERROR: operator does not exist: integer = character varying */
    ....;
    

    注意 table1.col4 上的 varchar 类型转换。

    另请注意,类型转换可能会使您在该列上的索引不可用,并且会降低性能,这非常糟糕。更好的解决方案是查看您是否可以永久更改两种列类型中的一种以匹配另一种。从字面上改变您的数据库设计。

    或者您可以使用自定义的 不可变 函数在转换后的值上创建索引,该函数将值转换到列上。但这也可能证明不是最理想的(但比现场直播更好)。

    【讨论】:

    • 确切地说是 PostgreSQL 在 8.2 中实现了“魔法”。它在 8.3 中停止了。
    • 解决了我的问题。另外,我发现使用“野兽”最有趣。
    猜你喜欢
    • 2011-07-03
    • 2015-10-02
    • 1970-01-01
    • 2021-11-15
    • 2018-12-16
    • 2016-11-13
    • 2012-05-19
    • 2014-04-05
    相关资源
    最近更新 更多