之间如何选择?

发布于 2024-08-13 18:31:17 字数 985 浏览 8 评论 0原文

我有一张表,称为 Level。

id  |  level  |  points(minimum)  
-------------------------
1   |  1      |  0 
2   |  2      |  100 
3   |  3      |  200 

假设我有 189 积分,我如何查看用户处于哪个级别?

编辑:

选择最佳答案。现在我通过在 SELECT 查询之前添加 EXPLAIN 来比较请求,我得到以下结果:

id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8   |  Using where


id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8   |  Using where; Using filesort

我如何知道哪个更好或更快?

I have a table, called Level.

id  |  level  |  points(minimum)  
-------------------------
1   |  1      |  0 
2   |  2      |  100 
3   |  3      |  200 

Let say I have 189 points, how do i check which level the user in?

EDIT:

Best answer chosen. Now I am comparing the request by adding EXPLAIN before the SELECT query, i have this result:

id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8   |  Using where


id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8   |  Using where; Using filesort

How do i know which one is better or faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

忱杏 2024-08-20 18:31:17

如果您要查找玩家当前所在的级别,则需要选择积分要求低于玩家当前拥有的积分的最高级别:

select max(level) from level where points <= 189;

如果每个级别都有 min_points 和 max_points amount:

id | level | min_points | max_points
------------------------------------
1  |   1   | 0          | 99
2  |   2   | 100        | 199
3  |   3   | 200        | 299

那么你的查询就不需要聚合:

select * from level where min_points <= 189 && max_points > 189;

编辑:呃,今晚我一直搞乱我的 SQL,哈哈。

If you're looking for the level that the player is currently in, you want to select the maximum level with a points requirement less than the points the player currently has:

select max(level) from level where points <= 189;

This may work better if each level has a min_points and max_points amount:

id | level | min_points | max_points
------------------------------------
1  |   1   | 0          | 99
2  |   2   | 100        | 199
3  |   3   | 200        | 299

Then your query wouldn't need to aggregate:

select * from level where min_points <= 189 && max_points > 189;

Edit: ugh, I keep messing up my SQL tonight, LOL.

鸩远一方 2024-08-20 18:31:17

这根本不需要“之间”或任何类型的聚合函数。您可以只选择小于点的所有行,按降序排序,然后第一行应该是正确的行。

select level from Level where points <= 189 order by point desc limit 1

(假设 MySQL ..如果您没有 MySQL,“限制”可能不起作用)

This wouldn't require a 'between' or any kind of aggregate function at all.. you could just select all rows that are less than the points, sort descending, and then first row should be the correct one.

select level from Level where points <= 189 order by points desc limit 1

(Assuming MySQL .. if you do not have MySQL, 'limit' may not work)

赠我空喜 2024-08-20 18:31:17

我不太确定你的意思,但我认为这就是你想要的:

SELECT `level` FROM `Level` WHERE `points`=189

I'm not quite sure what you mean but I think this is what you want:

SELECT `level` FROM `Level` WHERE `points`=189
迷鸟归林 2024-08-20 18:31:17
select max(level) from level where points <= 189

这假设表中的“分数”字段是达到该级别的最低分数。

select max(level) from level where points <= 189

This assumes the 'points' field in the table is the mininum points ot achieve that level.

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