Given the following example data:
mysql> select * from test2;
+-------+--------+
| name | status |
+-------+--------+
| bob | 0 |
| jim | 2 |
| karen | 5 |
| jane | 1 |
| roy | 7 |
+-------+--------+
I want to come up with a query which will split out the value of the status column into a pseudo-boolean value column for each individual bit of the integer.
What I do now is to have the application generate a query that uses a case statement and bitwise and operator for each column to get the result, like so:
mysql> select
-> name,
-> (case when (status & 1) then 'yes' else 'no' end) as bit1,
-> (case when (status & 2) then 'yes' else 'no' end) as bit2,
-> (case when (status & 4) then 'yes' else 'no' end) as bit4,
-> (case when (status & 8) then 'yes' else 'no' end) as bit8
-> from test2;
+-------+------+------+------+------+
| name | bit1 | bit2 | bit4 | bit8 |
+-------+------+------+------+------+
| bob | no | no | no | no |
| jim | no | yes | no | no |
| karen | yes | no | yes | no |
| jane | yes | no | no | no |
| roy | yes | yes | yes | no |
+-------+------+------+------+------+
5 rows in set (0.00 sec)
Now this works, and does so efficiently since there are no sub queries or joins, but it's not very elegant.
I would like a more elegant solution to do this dynamically without having to create a case statement and test for every bit.
This is example data with only a few bits, but in the implementation proper, there will be tens of thousands of rows, and many more bits that can be set.
Bonus:
Base the generation of the columns (column name and test) off of a table containing the definition of each bit like the following:
mysql> select * from bit_spec;
+-----+------+
| bit | desc |
+-----+------+
| 1 | bit1 |
| 2 | bit2 |
| 4 | bit3 |
| 8 | bit4 |
+-----+------+
Currently, the application handles this, and it's not a hard requirement that this processing be done by the MySQL server instead, but it would be advantageous if it was.
Here's the SQL Fiddle containing the example data, example bit definition table, and my current solution.