之间如何选择?
我有一张表,称为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要查找玩家当前所在的级别,则需要选择积分要求低于玩家当前拥有的积分的最高级别:
如果每个级别都有
min_points 和
max_points
amount:那么你的查询就不需要聚合:
编辑:呃,今晚我一直搞乱我的 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:
This may work better if each level has a
min_points
andmax_points
amount:Then your query wouldn't need to aggregate:
Edit: ugh, I keep messing up my SQL tonight, LOL.
这根本不需要“之间”或任何类型的聚合函数。您可以只选择小于点的所有行,按降序排序,然后第一行应该是正确的行。
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)
我不太确定你的意思,但我认为这就是你想要的:
I'm not quite sure what you mean but I think this is what you want:
这假设表中的“分数”字段是达到该级别的最低分数。
This assumes the 'points' field in the table is the mininum points ot achieve that level.