0

Sometimes there is a need to have multiple values in one variable or database field, even though that violates relational normalization principles. In python and other languages that support lists, that's easy. In others it is not. See insert multiple values in single attribute

One common technique is to concatenate values into a comma delimited string: "1,2,3" or "English,French,Spanish" and then extracting values by parsing.

When the valid values come from an enumerated list, is there another way that does not require parsing?

4

2 回答 2

1

Yes. Use prime numbers, multiply them together, then factor them out.

  1. The field type to use is integer or large integer

  2. Use code values that are prime numbers
    • 3 == English
    • 5 == French
    • 7 == Spanish
    • 11 == Italian

  3. Store the product of all that apply into the field.
    • 21 == English and Spanish
    • 385 == French, Spanish and Italian

  4. Use modulo functions to determine which values are in the field

    if ( field % 3 == 0 ) {  english() ;}
    if ! (field % 5) { french() ;}
    =IF(NOT(MOD(A203,5)),"French","")
    


  5. The same value can appear multiple times
    • 9 == English, English

I first used this technique to store dimensions.

  • 3 == time
  • 5 == length
  • 7 == mass
  • 11 == charge
  • 13 == temperature
  • 17 == moles

For example, a "first moment" lever-arm would have a dimension value of 35 == mass * length. To store fractional dimensions in an integer, I multiplied fractional dimensions by the product of all of them and dealt with it in processing.

  • 255255 == 3*5*7*11*13*17
  • force == mass * length / (second^2)
  • force == ( 7 * 5 / ( 3 * 3 ) ) * 255255 * 255255
  • force == 253381002875

The reason I used integers was to avoid dealing with invalid equality comparisons due to rounding errors.

Please do not ask for the code to extract the fractional dimensions. All this was 40 years ago in APL/360.

于 2013-03-05T20:43:02.590 回答
0

如果您不需要允许相同值的倍数,那么您可以使用位图。根据是否有多达 8、16、32、64 或 128 个允许值,它们可以适合 1、2、4、8 或 16 字节整数。

于 2013-03-05T20:59:44.280 回答