我的表格如下:
服务:
+----+-----------+------------+-------------+
| id | name | service_id | description |
+----+-----------+------------+-------------+
| 1 | CHAT | 1 | |
| 2 | SMS | 1 | |
+----+-----------+------------+-------------+
service_features:
+----+------------+------------+-------------+
| id | service_id | name | description |
+----+------------+------------+-------------+
|10 | 1 | Audio | |
|20 | 1 | Video | |
|30 | 2 | BULKSMS | |
+----+------------+------------+-------------+
client_services:
+----+-----------+------------+----------------------------------+---------------------------------------------+
| id | client_id | service_id |service_features_id |service_values |
+----+-----------+------------+----------------------------------+---------------------------------------------+
|100 | 1000 | 1 |features:{10:enable, 20:disable} |values:{data_transfer:102400, quota:204800} |
|200 | 1001 | 2 |features:{30:enable} |values:{total_sms:100000, users:800} |
|300 | 1001 | 1 |features:{10:disable, 20:enable} |values:{quota:204800} |
+----+-----------+------------+----------------------------------+---------------------------------------------+
现在我只想从mysql select query获取任何密钥的服务值,即client_id 1000的data_transfer或quota。 答案应分别为102400或204800
有没有更好的方法来形式化表格或存储信息。请建议。
答案 0 :(得分:1)
绝对有更好的方法。您的架构需要另外两个表,client_services_features
和client_services_values
,而不是client_services
。 client_services_features
包含以下列:
client_services_values
包含以下列:
每当您找到一个保持连续内容列表的表结构时,通常需要为列表创建一个单独的表。当列表包含id时,您总是希望为列表创建一个separete表。
答案 1 :(得分:0)
在列中存储值列表,json或xml使得查询特定数据非常困难。通常最好将数据分成不同的行。
我会制作另外两张表
client_services_values:
+----+-----------+------------+---------------------------------+
| id | client_id | service_id |service_type | service_value |
+----+-----------+------------+---------------------------------+
|1 | 1000 | 1 |data_transfer | 102400 |
|2 | 1000 | 1 |quota | 204800 |
|3 | 1001 | 2 |total_sms | 100000 |
|4 | 1001 | 2 |users | 800 |
|5 | 1001 | 1 |quota | 204800 |
+----+-----------+------------+---------------------------------+
和client_features
+----+-----------+------------+-----------------+
| id | client_id | feature_id | feature_value |
+----+-----------+------------+-----------------+
|1 | 1000 | 10 |enable |
|2 | 1000 | 20 |disable |
|3 | 1001 | 30 |enable |
|4 | 1001 | 10 |disable |
|5 | 1001 | 20 |enable |
+----+-----------+------------+-----------------+