How to Store Multiple Options selected by User in a Table

前端 未结 3 1727
感动是毒
感动是毒 2021-01-23 14:50

So I want my users to be able to restrict who may contact them.

There are several factors they should be able to filter, including Age (e.g. Must be between 18 - 29), in

相关标签:
3条回答
  • 2021-01-23 15:21

    You can try with this:

    criterium
    ------------
    user_id type          value
    1       AGE_MIN       18
    1       AGE_MAX       29
    1       INCOME_MIN    25000
    1       INCOME_MAX    60000
    1       DRUGS         Marijuana
    1       DRUGS         Meth
    
    0 讨论(0)
  • 2021-01-23 15:24

    No, don't store the values in CSV format in the database. Instead create a join table called user_drug and store one row for each user/drug combination:

    user
    id    name  income
    1     Foo   10000
    2     Bar   20000
    3     Baz   30000
    
    drug
    id    name
    1     Marijuana
    2     Cocaine
    3     Heroin
    
    user_drug
    user_id drug_id
    1       1
    1       2
    2       1       
    2       3
    3       3
    
    0 讨论(0)
  • 2021-01-23 15:43

    A DB column (at least theorethically) should NOT hold multiple values. Unfortunately, there are some programmers that store multiple values in a single column (values separated by comma for examples) - those programmers (in most cases) destroy the concept of DB and SQL.

    I suggest you to read about Database Normalization to get a start in organizing your tables. And, do your best to achieve the Codd's Third Normal Form

    0 讨论(0)
提交回复
热议问题