Situation
I want to convert certain status fields within my PHP/MySQL application to handle more than one at any time. Take below what is currently an ENUM field as an example:
STATUS_ACTIVE
STATUS_FROZEN
STATUS_PENDING
STATUS_FLAGGED
STATUS_DELETED
Currently, it will only support one at a time. However by converting the set in to binary, I end up up with a set of strings.
1 STATUS_ACTIVE
10 STATUS_FROZEN
100 STATUS_PENDING
1000 STATUS_FLAGGED
10000 STATUS_DELETED
Now, when it comes to filtering PHP wise, I have no issues. However, this introduces a large (and unnecessary) overhead. It is very possible an entry could end up as
10010
01110
etc.
Question
I’ve done some digging around and the best piece of information I have found was actually on stackoverflow (ironically) here. Can someone please explain the actual syntax used there or point me to a correct MySQL manual page.
It’s not very clear just how one would hypothetically go about these situations:
Changing only the third bit
Searching for 1 on only the fourth bit
Updating two binary flags at the same time
It would be really appreciated to have even a knock in the right direction, thanks for reading.
Edit: I have figured out most of the syntax. My question now: is it possible to change more than one bit at a time without having to redefine the whole thing?
I.e
100 -> 1011
1 -> 101
Answering your updated question. For example we need 3 and 5 bits.
If you need to SET 3 and 5 bits you should
ORyour field with10100If you need to CLEAR 3 and 5 bits you should
ANDyour field with11101011 = NOT(10100)