2010-02-01 6 views
4

J'ai cette requêteBit de masquage dans Postgres

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

et je reçois l'erreur suivante:

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.

Le models_mask est un entier dans la base de données. Comment puis-je réparer cela.

Merci!

Répondre

10

Consultez le docs on bit operators pour pg.

essentiellement & ne fonctionne que sur deux comme types (généralement peu ou int), alors model_mask devra être CAST ed de varchar à peu comme quelque chose de raisonnable ou int:

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

Vous pouvez savoir quels types d'opérateur travaille avec l'aide \doS dans psql

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 

Voici un exemple rapide pour plus d'informations

# 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

Excellent! Merci pour la solution et l'explication. –