2010-02-01 44 views
4

掩蓋我有這個疑問位Postgres裏

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

和我得到以下錯誤:

PGError: ERROR: operator does not exist: character varying & integer
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

的models_mask是數據庫中的一個整數。我怎樣才能解決這個問題。

謝謝!

回答

10

查看docs on bit operators的頁碼。

本質&僅適用於兩個相同類型(通常位或INT),所以model_mask將不得不從varchar CAST版的東西像合理位或INT:

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

你可以找出哪些類型的操作使用\doSpsql

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 
+0

非常好!感謝您的解決方案和解釋。 – 2010-02-01 22:26:00