【问题标题】:Bit masking in PostgresPostgres 中的位掩码
【发布时间】:2010-02-01 21:39:25
【问题描述】:

我有这个问题

SELECT * FROM "functions" WHERE (models_mask & 1 > 0)

我收到以下错误:

PGError:错误:运算符不存在:字符变化和整数
提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。

models_mask 是数据库中的一个整数。我该如何解决这个问题。

谢谢!

【问题讨论】:

    标签: postgresql bitwise-operators


    【解决方案1】:

    查看docs on bit operators 以获取 Pg。

    基本上& 只适用于两种类似的类型(通常是位或整数),所以model_mask 必须从 varchar 到 CASTed 到像位或整数这样合理的类型:

    models_mask::int & 1-或-models_mask::int::bit & b'1'

    您可以在psql 中使用\doS 找出运算符使用的类型

    pg_catalog | &    | bigint                      | bigint                      | bigint                      | bitwise and
    pg_catalog | &    | bit                         | bit                         | bit                         | bitwise and
    pg_catalog | &    | inet                        | inet                        | inet                        | bitwise and
    pg_catalog | &    | integer                     | integer                     | integer                     | bitwise and
    pg_catalog | &    | smallint                    | smallint                    | smallint                    | bitwise and
    

    这里有一个简单的例子来了解更多信息

    # SELECT 11 & 15 AS int, b'1011' & b'1111' AS bin INTO foo;
    SELECT
    
    # \d foo
          Table "public.foo"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     int    | integer | 
     bin    | "bit"   | 
    
    # SELECT * FROM foo;
     int | bin  
    -----+------
      11 | 1011
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多