How can I store an array of booleans in a MySql database?

In my case, each "element" either has a property or not. Properties can be several hundred, so I need, say, max 1000 true / false bits per element.

Is there a way to store these bits in one element field?

+3
source share
4 answers

If you are looking for a way to do this with a search method, then no.

Several search methods (involving more than one column and / or table):

  • Use a bunch of SET columns . You are limited to 64 points (on / off) in the set, but you might figure out a way to group them.
  • 3 : Items (id,...), FlagNames (id, name) ItemFlags (item_id, flag_id). .

, , , , , , , , char, varchar.

  • , (/ PHP).
  • "y" "n" .
  • Bit-pack (8 ) MySQL . ( , char [x], varchar [x]) .
+3

- :

Properties
ID, Property
1, FirsProperty
2, SecondProperty

ItemProperties
ID, Property, Item
1021, 1, 10
1022, 2, 10

, .

+3

char (1000) [ynnynynynynynynynny...] . (, ), char (64) [ ].

64, SET , , , .

, , .., .

, , , .

, VARCHAR , , , . ( - )

0

, , :

$bools = array(0,1,1,0,1,0,0,1);
$for_db = serialize($array);

// Insert the serialized $for_db string into the database. You could use a text type
// make certain it could hold the entire string.
// To get it back out:

$bools = unserialize($from_db);

, .

"item", " " "". Entity Attribute Value .

0
source

Source: https://habr.com/ru/post/1752571/


All Articles