MySQL 搜索查询

发布于 2024-10-07 08:49:53 字数 752 浏览 1 评论 0原文

我正在创建一个系统来存储用户统计数据,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雪落纷纷 2024-10-14 08:49:53

这种设计的问题在于,您在 Stat_Options 表中混合了定量值(身高、体重)和定性值(eye_colour)。如果您确实想要这样做,请将定量和定性值分为两列。

Table Stat_Options
==================================
id   stat_id   name   value  unit
===  =======   =====  =====  =====
  1        1   170cm    170     cm
  2        1   172cm    172     cm
  3        1   174cm    174     cm
  4        2    60kg     60     kg
  5        2    65kg     65     kg
  6        3   black   NULL  black

然后按照其他建议,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.

Table Stat_Options
==================================
id   stat_id   name   value  unit
===  =======   =====  =====  =====
  1        1   170cm    170     cm
  2        1   172cm    172     cm
  3        1   174cm    174     cm
  4        2    60kg     60     kg
  5        2    65kg     65     kg
  6        3   black   NULL  black

Then as other suggested, JOIN tables to get values BETWEEN x AND y.

云朵有点甜 2024-10-14 08:49:53

恕我直言,您应该仅在属性具有有限数量值(例如眼睛颜色)的情况下使用像 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 your User_Stats table instead.

请你别敷衍 2024-10-14 08:49:53

这样做不是一个好主意。例如,使用 Stat_option 绑定下拉列表,以防您有一个数字从不经常更改值。

为什么不直接向用户统计表添加一个整数字段与高度?

但如果你真的想要你可以这样做:

SELECT * FROM USER_STATS 
INNER JOIN STAT_OPTION 
ON USER_STATS.height in (
 SELECT ID FROM Stat_Options
 WHERE NAME BETWEEN 170 AND 175)

或者类似的事情,我不常用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:

SELECT * FROM USER_STATS 
INNER JOIN STAT_OPTION 
ON USER_STATS.height in (
 SELECT ID FROM Stat_Options
 WHERE NAME BETWEEN 170 AND 175)

Or something like that, i'm not usual with MySql..

别把无礼当个性 2024-10-14 08:49:53
SELECT user_id FROM User_Stats WHERE height BETWEEN 170 AND 172;

我猜是那个。

但对于表的结构,我可以建议如下:

  1. 不需要 Stats 表,因为它可以毫无问题地包含在 User_Stats 表中。这样您就可以更好地标准化表的使用。

  2. 如果且仅当身高和体重的值可以超过您的 3(例如 180、183、192、200、110...),那么我建议与第 1 点相同。表并包含在 User_Stats 表中,因为该值可以更改,并且实际上没有必要为此创建一个新表。这样您就可以更快地进行搜索,并轻松地在单个表格中比较您想要的任何内容。

SELECT user_id FROM User_Stats WHERE height BETWEEN 170 AND 172;

i would guess that one.

But for the structure of your tables i can suggest the following:

  1. 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.

  2. 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.

二智少女猫性小仙女 2024-10-14 08:49:53

从您的示例中,您是否可以通过查询获取身高在 x 和 y 之间的所有用户:

Select user_id from User_Stats where User_Status.height between x and y

From your example, could you get all users with a height between x and y by querying:

Select user_id from User_Stats where User_Status.height between x and y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文