mys*_*ner 1 mysql select points rank
我有一张叫做Level的桌子.
id | level | points(minimum)
-------------------------
1 | 1 | 0
2 | 2 | 100
3 | 3 | 200
Run Code Online (Sandbox Code Playgroud)
假设我有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
Run Code Online (Sandbox Code Playgroud)
我怎么知道哪一个更好或更快?
如果您正在寻找玩家当前所处的等级,您需要选择点数要求低于玩家当前所拥有的点数的最高等级:
select max(level) from level where points <= 189;
Run Code Online (Sandbox Code Playgroud)
如果每个级别都有a min_points和max_points金额,这可能会更好:
id | level | min_points | max_points
------------------------------------
1 | 1 | 0 | 99
2 | 2 | 100 | 199
3 | 3 | 200 | 299
Run Code Online (Sandbox Code Playgroud)
然后您的查询将不需要聚合:
select * from level where min_points <= 189 && max_points > 189;
Run Code Online (Sandbox Code Playgroud)
编辑:呃,我今晚一直搞乱我的SQL,大声笑.
| 归档时间: |
|
| 查看次数: |
110 次 |
| 最近记录: |