存储在MySQL中的二进制字符串 [英] Storing binary string in MySQL
问题描述
我已经开发了一个小型的二元信号系统,我们的管理中心。它允许我们设置项目分配有多个选项,而不必存储有一个表与几个领域。
I've developed a small binary flag system for our admin centre. It allows us to set items to have multiple options assigned to them, without having to store have a table with several fields.
一旦选项被转换成与位运算符二元,我们最终会像10000或10010的一个选项是所有好。这样做可以使我们能够不断添加的选项,而无须重新编写其值是10010&安培; (1<&4;),我知道我们的东西打开
Once the options are converted into binary with bitwise operators, we'd end up with an option like 10000 or 10010 which is all good. Doing it this way allows us to keep adding options, but without having to re-write which value is which, 10010 & (1 << 4) and I know that we have something turned on.
问题是但在存储我们的MySQL表该数据。我试过几个字段类型,但他们都不让我来执行一个查询,例如,
The problem however is storing this data in our MySQL table. I've tried several field types, but none of them are allowing me to perform a query such as,
SELECT * FROM _table_ x WHERE x.options & (1 << 4)
建议?
推荐答案
要检查是否有位被设置您的查询必须是:
To check if a bit is set your query needs to be:
SELECT * FROM _table_ x WHERE x.options & (1 << 4) != 0
和检查,如果它不是设置:
And to check if it's not set:
SELECT * FROM _table_ x WHERE x.options & (1 << 4) = 0
更新:这里是如何设置一个单独的位:
Update: Here's how to set an individual bit:
UPDATE table SET options = options | (1 << 4)
要清除单个位:
UPDATE table SET options = options &~ (1 << 4)
您也可以用二进制字符串将它们全部一次:
You can also set them all at once with a binary string:
UPDATE table SET options = b'00010010'
这篇关于存储在MySQL中的二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!