How to store multiple values in single column where use less memory?

后端 未结 4 1490
滥情空心
滥情空心 2021-01-02 21:40

I have a table of users where 1 column stores user\'s \"roles\". We can assign multiple roles to particular user.

Then I want to store role IDs in

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 22:26

    From your question what I got,

    Suppose, you have to table. one is "meal" table and another one is "combo_meal" table. Now I think you want to store multiple meal_id inside one combo_meal_id without separating coma[,]. And you said that it'll make your DB to more standard.

    If I not getting wrong from your question then please read carefully my suggestion bellow. It may be help you.

    First think is your concept is right. Definitely it'll give you more standard DB.

    For this you have to create one more table [ example table: combo_meal_relation ] for referencing those two table data. May be one visible example will clear it.

    meal table
    
    +------+--------+-----------+---------+
    |  id  |  name  | serving   |  price  |
    +------+--------+-----------+---------+
    |  1   | soup1  |  2 person |  12.50  |
    +------+--------+-----------+---------+
    |  2   | soup2  |  2 person |  15.50  |
    +------+--------+-----------+---------+
    |  3   | soup3  |  2 person |  23.00  |
    +------+--------+-----------+---------+
    |  4   | drink1 |  2 person |  4.50   |
    +------+--------+-----------+---------+
    |  5   | drink2 |  2 person |  3.50   |
    +------+--------+-----------+---------+
    |  6   | drink3 |  2 person |  5.50   |
    +------+--------+-----------+---------+
    |  7   | frui1  |  2 person |  3.00   |
    +------+--------+-----------+---------+
    |  8   | fruit2 |  2 person |  3.50   |
    +------+--------+-----------+---------+
    |  9   | fruit3 |  2 person |  4.50   |
    +------+--------+-----------+---------+
    
    combo_meal table
    
    +------+--------------+-----------+
    |  id  |  combo_name  | serving   |
    +------+--------------+-----------+
    |  1   |   combo1     |  2 person |
    +------+--------------+-----------+
    |  2   |   combo2     |  2 person |
    +------+--------------+-----------+
    |  4   |   combo3     |  2 person |
    +------+--------------+-----------+
    
    combo_meal_relation
    
    +------+--------------+-----------+
    |  id  | combo_meal_id|  meal_id  |
    +------+--------------+-----------+
    |  1   |      1       |      1    |
    +------+--------------+-----------+
    |  2   |      1       |      2    |
    +------+--------------+-----------+
    |  3   |      1       |      3    |
    +------+--------------+-----------+
    |  4   |      2       |      4    |
    +------+--------------+-----------+
    |  5   |      2       |      2    |
    +------+--------------+-----------+
    |  6   |      2       |      7    |
    +------+--------------+-----------+
    

    When you search inside table then it'll generate faster result.

    search query:

    SELECT  m.*
    FROM    combo_meal cm
    JOIN    meal m
    ON      m.id = cm.meal_id
    WHERE   cm.combo_id = 1
    

    Hopefully you understand :)

提交回复
热议问题