从位字段中按索引获取一位,mysql

发布于 2024-12-20 01:19:55 字数 421 浏览 4 评论 0原文

有没有办法从 mysql 中的位字段获取一位(或布尔值)?

我想出的唯一解决方案是使用以下内容: 我想要位置三的位值。

SELECT bit_count(bin(column) & b'100') AS bitValue FROM table;

该查询的输出要么是 1,要么是 0。但它看起来不太好。 有没有更好的方法来获取位字段中特定位置的位值? 读取位值将在我的 Web 应用程序中大量完成,您认为获取我上面使用的值的方式会对性能产生问题吗?我的意思是同时使用 bit_count 和 BIN() 以及“and”运算符..

我正在寻找类似的内容:

SELECT column(3) FROM table;

感谢您的阅读!

马蒂亚斯。

Is there a way to get one bit (or bool) from a bit field in mysql?

The only solution i've came up with is to use the following:
i want the bit value at position three.

SELECT bit_count(bin(column) & b'100') AS bitValue FROM table;

The output from this query is either a one or a zero. But it does not look so nice.
Is there a better way to get the bit value at a specific position in a bit field?
Reading the bit values would be done heavily at my web application, do you think that the way of getting the value that i used above would be a problem for the performance?? i mean with both a bit_count and a BIN() and "and" operator..

I am looking for something like:

SELECT column(3) FROM table;

Thanks for reading!

Mattias.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-12-27 01:19:55

如果您担心读取性能并且您感兴趣的位是固定的,请尝试提取该位并将其插入布尔列中。如果您尝试提取的位是动​​态的,我认为您的解决方案是最佳的。

If you are worried about read performance and the bit you are interested in is fixed, try to extract the bit and insert it into a boolean column. If the bit you are trying to extract is dynamic, I think your solutions is optimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文