Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

krie9er's avatar

MySQL: How to select from a binary field

Hello people :-)

I'm currently working on some Dataming tasks and i have a question realated to the use of the SQL-Statement where in mysql.

In my Database is a field, which contains binary keys.

F.e.: 0100101101

In a select I want to get all Datarows, where the key contains the decimal "4", this means every keys with this structure: xxxxxxxx1xx

with php and an array it would be really easy, but i consider about if it is possible to make a select-statement which would have a better performance compared to a php script.

my current idea is to check if the third place is a 1 or a 0, but i have no idea how to write this as an sql statement :-/

I hope you have a nice idea, because i'm running out of ideas :-(

Thanks :-)

0 likes
4 replies
krie9er's avatar

Thanks for the quick reply but how can i use this to get all row's where for example the third bit is set?

So i've stored some values inside a table with a bigint field, now i want to get all rows, where the third bit (4) is set.

d3xt3r's avatar

Thanks for the quick reply but how can i use this to get all row's where for example the third bit is set?

The answer is all there, did you even care to look at it. Anyway,

SELECT * from table where CONV(key,2,10) & 4 = 4
1 like
krie9er's avatar

Hey really Thanks for that!

I had to refresh my understanding of the bitwise And operator....

I'm sorry for being so silly :-X

But anyway, thanks for the example :-)

Please or to participate in this conversation.