postgresql Postgres 中的位掩码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2180266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bit masking in Postgres
提问by Silviu Postavaru
I have this query
我有这个查询
SELECT * FROM "functions" WHERE (models_mask & 1 > 0)
and the I get the following error:
我收到以下错误:
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.
PGError:错误:运算符不存在:字符可变和整数
提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。
The models_mask is an integer in the database. How can I fix this.
models_mask 是数据库中的整数。我怎样才能解决这个问题。
Thank you!
谢谢!
回答by Evan Carroll
Check out the docs on bit operatorsfor Pg.
Essentially &
only works on two like types (usually bit or int), so model_mask
will have to be CAST
ed from varchar to something reasonable like bit or int:
基本上&
只适用于两种类似的类型(通常是 bit 或 int),所以model_mask
必须CAST
从 varchar 编辑为合理的东西,比如 bit 或 int:
models_mask::int & 1
-or- models_mask::int::bit & b'1'
models_mask::int & 1
- 或-models_mask::int::bit & b'1'
You can find out what types an operator works with using \doS
in psql
你可以找出哪些类型的运营商工作使用\doS
中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
Here is a quick example for more information
这是一个快速示例以获取更多信息
# 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