我怎么可以存储布尔值的MySQL数据库的阵列? [英] How can I store an array of boolean values in a MySql database?
问题描述
在我的情况下,每一个项目要么有一个属性,还是不行。属性可以是几百个,所以我会需要,也就是说,每个项目最高1000真/假位。
In my case, every "item" either has a property , or not. The properties can be some hundreds, so I will need , say, max 1000 true/false bits per item.
有没有这些位存储在项目的一个字段的方法吗?
Is there a way to store those bits in one field of the item ?
推荐答案
如果你正在寻找一种方式,在某种程度上这是搜索要做到这一点,那么没有。
If you're looking for a way to do this in a way that's searchable, then no.
一对夫妇的搜索方法(涉及超过1列和/或表):
A couple searchable methods (involving more than 1 column and/or table):
- 使用一堆 SET 列。你限制为64个项目(上/取舍)在一组,但你可能中央社想出一个办法将它们分组。
- 使用3个表:项目(ID,...),FlagNames(ID,姓名),和数据透视表ItemFlags(ITEM_ID,FLAG_ID)。然后,您可以查询商品与加入。
- Use a bunch of SET columns. You're limited to 64 items (on/offs) in a set, but you cna probably figure out a way to group them.
- Use 3 tables: Items (id, ...), FlagNames(id, name), and a pivot table ItemFlags(item_id, flag_id). You can then query for items with joins.
如果你不需要它是搜索,那么所有你需要的是你把它放在数据库之前,序列化数据的方法,和反序列化它,当你拉出来,然后用CHAR或VARCHAR列。
If you don't need it to be searchable, then all you need is a method to serialize your data before you put it in the database, and a unserialize it when you pull it out, then use a char, or varchar column.
- 内建在你的语言使用的设施(PHP的序列化/反序列化)。
- 串连了一系列的Y和N的字符在一起。
- 使MySQL数据库呼叫前位包你在客户端的值转换成字符串(8位字符)和检索出数据库的数据时,解压它们。这是最有效的存储机制(如果所有的行是相同的,使用的char [X],而不是为varchar [X])将不被搜索的,稍微复杂code中的数据的费用。
这篇关于我怎么可以存储布尔值的MySQL数据库的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!