MOnsDaR
MOnsDaR

Reputation: 8664

Store a field of multiple Booleans

My point is the following:
I have a MySQL DB where events are stored. These events could be on different days of the week. You could say it looks like the following:

Day | Active
Mon | yes
Tue | yes
Wed | no
Thu | no
Fri | yes
Sat | no
Sun | no

Now I'm not entirely sure on how I could save this into the DB. Of course there are several possibilities but I see disadvantages on everything I could think of:

In C++ these data could be stored into a std::bitset. I bet there is something which could be used for this in MySQL... Please point me to it :)

PS:
I don't know if it is important: The data will be read with PHP and served as JSON to the outer world.

Upvotes: 0

Views: 102

Answers (3)

75inchpianist
75inchpianist

Reputation: 4102

you should prolly use the int idea. In your php you can define the masks as constants, and than use bitwise operations to figure out which flags are set.

So rather than doing something like

myvar & 0x010

it would look more like you are operating with a constant

myvar & IS_MONDAY

Upvotes: 2

clementcyu
clementcyu

Reputation: 1

If your entry is merely to store weekly events with the input you provided, why not create a VARCHAR field and store it as a JSON string: { "Mon":"yes", "Tue":"yes", "Wed":"no", "Thu":"no", "Fri":"yes", "Sat":"no", "Sun":"no" }

Upvotes: 0

Stormherz
Stormherz

Reputation: 376

You can use an SET type (http://dev.mysql.com/doc/refman/5.0/en/set.html). For example, that's what you table might look like:

+-------------+--------------------+
| event_name  | days               |
+-------------+--------------------+
| some event  | 'monday,friday'    |
+-------------+--------------------+
| another one | 'tuesday'          |
+-------------+--------------------+

It is easy to select rows from that kind of table and it's really readable for anyone lurking in the database.

Upvotes: 3

Related Questions