简体   繁体   English

在MySQL数据库中存储位掩码值数组的最佳方法?

[英]Best way to store an array of bitmasked values in a MySQL database?

I am using a bitmask value to store a list of possible people in a row (1=John, 2=Paul, 4=George, 8=Ringo, 16=Pete, etc.) and that's working just fine, but in a certain situation I need to store this value for each day of the week. 我正在使用一个位掩码值来连续存储一个可能的人的列表(1 = John,2 = Paul,4 = George,8 = Ringo,16 = Pete等),并且工作正常,但在一定程度上情况我需要在一周的每一天存储此值。

I can easily do it by creating columns for each day (peopleSun, peopleMon, peopleTue, etc.) since while the number of people could change, the number of days in a week won't. 我可以通过每天创建一列(peopleSun,peopleMon,peopleTue等)来轻松完成此操作,因为尽管人数可以更改,但一周中的天数不会更改。 But it feels so... dirty, like I should be more efficient with it somehow. 但这感觉真...很脏,就像我应该以某种方式更有效。

I'm using it in connection with PHP, and so I realize I could create an array and encode it using serialize(), but that feels like it makes the database less accessible in the future. 我将其与PHP结合使用,因此我意识到我可以创建一个数组并使用serialize()对其进行编码,但是感觉这会使将来数据库的访问性降低。

Is there a better way of doing this, or should I just hardwire seven different columns and stop worrying about making my database ready to handle... eight days a week? 有没有更好的方法可以做到这一点,还是我应该只硬连接七个不同的列,而不必担心让我的数据库准备好……每周八天进行处理? (ugh, sorry I didn't plan for that, it was just too good to pass up) (哦,对不起,我没有为此计划,实在太好了,无法通过)

Okay, I wasn't thinking with portals relational databases. 好的,我不是在考虑 门户网站 关系数据库。

I created a second table with columns "people", "day" (1=Sun, 2=Mon, etc), and "recordID" (which is tied to the id of the row in the other table. 我创建了第二个表,其中包含列“ people”,“ day”(1 = Sun,2 = Mon等)和“ recordID”(与另一个表中的行的ID关联)。

Sometimes you just need to let these things simmer for a weekend, I guess... 我想有时候有时候你只需要让这些东西一下就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM