dongzanghong4379 2017-04-03 18:22
浏览 189

如何在数据库中存储可变长度数组

I'm looking for a way to save data based off a set of rows in another table, but I don't know how to set up the field. Think something similar to using the results of a mysql group_concat as the field. The data is based off the unique combination of rows, rather than one row or field.

What I need to be able to do is:

  1. Store the array itself in the database
  2. Store associated data about the array
  3. Retreive the array
  4. The ability to lookup data about the array using the data in the array

Some options I've thought about:

  1. Saving as an ordered set concatenated into a string.
  2. Saving the serialized array (serialized using php's serialize function).
  3. Saving the set as a hashed string using a reversible hash.

None of these options seem correct so I came here hoping someone has a better answer.

Background:

Supposed I have the following tables:

users {id, other unimportant fields}
products {id, other unimportant fields}
shipments {id, user_id, product_id, date, other unimportant fields}

I want to create a new table called assigned_products where the assigned product is based off of the unique combination of products they've received in the past. So assigned_products should look like:

assigned_products {set_of_products_received (array), product_id (data about the array)}

I don't know of a good way to store set_of_products_received in a database.

Example use:

Suppose I have 100 users who got product A, 100 users who got product B, and 100 users who got products A and B. Suppose then I wanted to give product B to everyone who got product A, product A to everyone who got product B, and product C to everyone who got product A and B. The assigned products table should look like:

+--------------------------+------------+
| set_of_products_received | product_id |
+--------------------------+------------+
| A                        | B          |
| B                        | A          |
| A, B                     | C          |
+--------------------------+------------+

I'm just looking for a better way of storing set_of_products_received

Reading this over I realize it's a bit hard to understand, but I don't really know the appropriate terms to describe this issue (probably why I'm having trouble finding solutions). I'll be happy to clarify if anyone has any questions.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度