MySQL 搜索查询
我正在创建一个系统来存储用户统计数据,如下所示:
Table Stats =========== id name ----------- 1 height 2 weight 3 eye_colour 4 etc...
Table Stat_Options ================== id stat_id name 1 1 170cm 2 1 172cm 3 1 174cm 4 2 60kg 5 2 65kg
我将 stat_options 的 Id 存储在用户表中,如下所示:
Table User_Stats ================ id user_id height weight eye_colour etc... --------------------------------------------------------- 1 10 3 5 7
存储 Id 背后的原因是 stat_option 的“名称”始终可以更改,并且用户记录不需要更改。
但看看这个,我认为这可能会带来问题,尤其是在搜索时。例如,如果我想搜索两个特定身高之间的所有用户?我想到的一个解决方案是在 Stat_Options 表中插入一个“值”字段,但我仍然不确定这是否可行。有什么建议吗?
I am creating a system to store user stat data as follows:
Table Stats =========== id name ----------- 1 height 2 weight 3 eye_colour 4 etc...
Table Stat_Options ================== id stat_id name 1 1 170cm 2 1 172cm 3 1 174cm 4 2 60kg 5 2 65kg
And I store the Ids of the stat_options in the user table as follows:
Table User_Stats ================ id user_id height weight eye_colour etc... --------------------------------------------------------- 1 10 3 5 7
The reasoning behind storing the Ids was that the 'name' of the stat_option can always be changed and the user record would not need to be changed.
But looking at this I think it may present a problem, especially when searching. For example if I want to search for all users between two specific heights? One solution that comes to mind is to insert a 'value' field in to the Stat_Options table, but I'm still unsure if that would work. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这种设计的问题在于,您在
Stat_Options
表中混合了定量值(身高、体重)和定性值(eye_colour)。如果您确实想要这样做,请将定量和定性值分为两列。然后按照其他建议,
JOIN
表来获取BETWEEN x AND y
值。The problem with this type of design is that you are mixing quantitative values (height, weight) with qualitative values (eye_colour) in your
Stat_Options
table. If you absolutely want this, break down quantitative and qualitative values into 2 columns.Then as other suggested,
JOIN
tables to get valuesBETWEEN x AND y
.恕我直言,您应该仅在属性具有有限数量值(例如眼睛颜色)的情况下使用像
Stat_Options
这样的查找表。尝试将这样的表用于(理论上)具有无限多个值的属性,例如身高和体重,正如您已经猜到的那样,会引起严重的头痛。我会将这些值直接存储在您的User_Stats
表中。IMHO, you should use a lookup table like your
Stat_Options
only for cases where the attribute has a finite number of values, like eye color. Trying to use a table like this for attributes that (theoretically) have an infinite number of values, like height and weight, will, as you've already guessed, cause major headaches. I'd store those values directly in yourUser_Stats
table instead.这样做不是一个好主意。例如,使用 Stat_option 绑定下拉列表,以防您有一个数字从不经常更改值。
为什么不直接向用户统计表添加一个整数字段与高度?
但如果你真的想要你可以这样做:
或者类似的事情,我不常用MySql..
This is not a good idea to do like this.. Use Stat_option to bind dropdownlist for exemple, in case you have a number never not often changing of values..
why Don't you just add a integer field to the User-stats table with the height?
But if you really want you can do:
Or something like that, i'm not usual with MySql..
我猜是那个。
但对于表的结构,我可以建议如下:
不需要 Stats 表,因为它可以毫无问题地包含在 User_Stats 表中。这样您就可以更好地标准化表的使用。
如果且仅当身高和体重的值可以超过您的 3(例如 180、183、192、200、110...),那么我建议与第 1 点相同。表并包含在 User_Stats 表中,因为该值可以更改,并且实际上没有必要为此创建一个新表。这样您就可以更快地进行搜索,并轻松地在单个表格中比较您想要的任何内容。
i would guess that one.
But for the structure of your tables i can suggest the following:
There is no need for the Stats table since it can be included with no problem inside the User_Stats Table. That way you would normalise better the usage for the tables.
IF and only IF the values for height and weight can be more than the 3 you have there (For example 180, 183, 192, 200, 110...) then i suggest the same as point 1. To drop the table and included inside the User_Stats table since this value can change and are not really necessary to make a new table just for that. That way you can do searches faster and easily comparing anything you want in a single table.
从您的示例中,您是否可以通过查询获取身高在 x 和 y 之间的所有用户:
From your example, could you get all users with a height between x and y by querying: