简体   繁体   English

用于在MySQL数据库中存储多个范围的表设计

[英]Table design for storing multiple ranges in MySQL database

I have several categories that I need to store in a database and present them to users. 我有几种类别需要存储在数据库中,并提供给用户。 Each one has a minimum of three to a maximum of four ranges. 每个范围至少有三个,最多四个范围。 Eg: 例如:

id | category_name | Range A |  Range B  |  Range C  |  Range D  
--------------------------------------------------------------------
 1 | Category1     | 0 - 200 | 200 - 450 | 450 - 750 | 750+
 2 | Category2     | 0 - 300 | 300 - 600 | 600+      |           
 3 | Category3     | 0 - 250 | 250 - 350 | 350 - 550 | 550+

When an user picks a category, he should then select a certain range that will be saved in the database. 当用户选择一个类别时,他应该选择一个要保存在数据库中的特定范围。

    name    | category_id | category_range
------------------------------------------
 niceuser30 | 2           | A
 hellouser1 | 1           | B

Considering that: 考虑到:

  • ranges cannot have gaps between them (eg if range A goes from 100 to 200, range B must start from 200) 范围之间不能有间隔 (例如,如果范围A从100到200,则范围B 必须从200开始)
  • each range must start from 0 每个范围必须从0开始
  • each range must be open ended (or half-open) 每个范围必须是开放式的(或半开放式的)

What would be the best design for a table to hold these values? 拥有这些值的表的最佳设计是什么?

I was thinking of using something akin to this 我当时正在考虑使用类似的东西

id | category_name | range_a | range_b | range_c | range_d
-------------------------------------------------------------
1  | Category1     | 1500    | 3000    | 5000    | 5000
2  | Category2     | 500     | 1000    | 1000    | 

and then elaborate the output before serving it to the user (if two ranges are equal the code sets the last one "ad infinitum", so the first one would be "0 - 1500, 1500 - 3000, 3000 - 5000, 5000+") but it seems dirty and prone to errors. 然后在将输出提供给用户之前详细说明输出(如果两个范围相等,则代码会设置最后一个“无穷大”,因此第一个将是“ 0-1500、1500-3000、3000-5000、5000+” ),但它似乎很脏 ,容易出错。

you could use 2 table eg Ranges: 您可以使用2个表格,例如Ranges:

   id  | RangeName| Start |  End |  Range  | 
    --------------------------------------------------------------------
     1 | A        | 0    | 1500 | 1500     |
     2 | B        | 1500 | 3000 | 1500     |           
     3 | C        | 3000 | 4000 | 1000     | 
     ....

and RangeCategories : RangeCategories

  id   | CategoryID|RangeId| 
    --------------------------------------------------------------------
     1 | 1         |   1   | 
     2 | 1         |   2   |          
     3 | 2         |   3   | 
     4 | 2         |   1   | 
     .....

And of course you will also have your categories Table: 当然,您还将获得categories表:

    id | CategoryName|...
   --------------------------------------------------------------------
     1 | Category1     | ...
     2 | Category2     | ...          
     3 | Category3     | ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM